dqcsim 0.4.1

DQCsim: Delft Quantum Classical Simulator
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
use super::*;
use crate::common::gates::UnitaryGateType;
use std::convert::TryFrom;

/// Helper function for the `dqcs_gate_new_predef*()` functions.
fn new_predef_helper(
    gate_type: dqcs_predefined_gate_t,
    qubits: Vec<QubitRef>,
    param_data: dqcs_handle_t,
) -> Result<dqcs_handle_t> {
    // Interpret gate type.
    let gate_type = UnitaryGateType::try_from(gate_type)?;

    // Interpret data.
    resolve!(optional param_data as pending ArbData);
    let data: ArbData = {
        if let Some(data) = param_data.as_ref() {
            let x: &ArbData = data.as_ref().unwrap();
            x.clone()
        } else {
            ArbData::default()
        }
    };

    // Construct the gate.
    let gate = insert(
        gate_type
            .into_gate_converter(None, 0., false)
            .construct(&(qubits, data))?,
    );

    // Delete consumed handles.
    if let Some(mut param_data) = param_data {
        delete!(resolved param_data);
    }

    Ok(gate)
}

/// Constructs a new predefined unitary gate.
///
/// `gate_type` specifies which kind of gate should be constructed.
///
/// `targets` must be a handle to a non-empty qubit set, containing at least
/// as many qubits as needed for the specified gate type. If more qubits are
/// specified, the rightmost qubits become the targets, and the remaining
/// qubits become control qubits to make a controlled gate.
///
/// `param_data` takes an optional `ArbData` object used to parameterize the
/// gate if necessary. If not specified, an empty object is used. Some of the
/// gate types are parameterized, and use values from this `ArbData` as
/// defined in the docs for `dqcs_predefined_gate_t`. Anything remaining in
/// the `ArbData` afterwards is placed in the gate object.
///
/// This function returns the handle to the gate, or 0 to indicate failure.
/// The qubit set and parameterization data (if specified) are consumed/deleted
/// by this function if and only if it succeeds.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_predef(
    gate_type: dqcs_predefined_gate_t,
    qubits: dqcs_handle_t,
    param_data: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        // Interpret qubits.
        resolve!(qubits as pending QubitReferenceSet);
        let qubit_vec: Vec<QubitRef> = {
            let x: &QubitReferenceSet = qubits.as_ref().unwrap();
            x.iter().cloned().collect()
        };

        // Call the helper for the rest of the logic.
        let gate = new_predef_helper(gate_type, qubit_vec, param_data)?;

        // Delete consumed handles.
        delete!(resolved qubits);
        Ok(gate)
    })
}

/// Constructs a new predefined unitary one-qubit gate.
///
/// This function is simply a shorthand for `dqcs_gate_new_predef()` with
/// one qubit in the `qubits` set, to make constructing one-qubit gates more
/// ergonomic. Refer to its documentation for more information.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_predef_one(
    gate_type: dqcs_predefined_gate_t,
    qa: dqcs_qubit_t,
    param_data: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        let qubits_vec = vec![QubitRef::from_foreign(qa)
            .ok_or_else(oe_inv_arg("0 is not a valid qubit reference"))?];
        new_predef_helper(gate_type, qubits_vec, param_data)
    })
}

/// Constructs a new predefined unitary two-qubit gate.
///
/// This function is simply a shorthand for `dqcs_gate_new_predef()` with
/// two qubit in the `qubits` set, to make constructing two-qubit gates more
/// ergonomic. Refer to its documentation for more information.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_predef_two(
    gate_type: dqcs_predefined_gate_t,
    qa: dqcs_qubit_t,
    qb: dqcs_qubit_t,
    param_data: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        let qubits_vec = vec![
            QubitRef::from_foreign(qa)
                .ok_or_else(oe_inv_arg("0 is not a valid qubit reference"))?,
            QubitRef::from_foreign(qb)
                .ok_or_else(oe_inv_arg("0 is not a valid qubit reference"))?,
        ];
        if qa == qb {
            inv_arg(format!("cannot use qubit {} twice", qa))?;
        }
        new_predef_helper(gate_type, qubits_vec, param_data)
    })
}

/// Constructs a new predefined unitary three-qubit gate.
///
/// This function is simply a shorthand for `dqcs_gate_new_predef()` with
/// three qubit in the `qubits` set, to make constructing three-qubit gates
/// more ergonomic. Refer to its documentation for more information.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_predef_three(
    gate_type: dqcs_predefined_gate_t,
    qa: dqcs_qubit_t,
    qb: dqcs_qubit_t,
    qc: dqcs_qubit_t,
    param_data: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        let qubits_vec = vec![
            QubitRef::from_foreign(qa)
                .ok_or_else(oe_inv_arg("0 is not a valid qubit reference"))?,
            QubitRef::from_foreign(qb)
                .ok_or_else(oe_inv_arg("0 is not a valid qubit reference"))?,
            QubitRef::from_foreign(qc)
                .ok_or_else(oe_inv_arg("0 is not a valid qubit reference"))?,
        ];
        if qa == qb || qa == qc {
            inv_arg(format!("cannot use qubit {} twice", qa))?;
        }
        if qb == qc {
            inv_arg(format!("cannot use qubit {} twice", qb))?;
        }
        new_predef_helper(gate_type, qubits_vec, param_data)
    })
}

/// Constructs a new unitary gate.
///
/// `targets` must be a handle to a non-empty qubit set. The qubits in this set
/// correspond with the supplied unitary matrix.
///
/// `controls` optionally specifies a set of control qubits. You may pass 0 or
/// an empty qubit set if you don't need control qubits.
///
/// `matrix` must be a handle to an appropriately sized matrix.
///
/// The supplied matrix is only applied to the target qubits if all the control
/// qubits are or will be determined to be set. For instance, to encode a
/// CCNOT/Toffoli gate, you can specify one target qubits, two control qubits,
/// and [0, 1; 1, 0] (X) for the matrix. This is equivalent to extending the
/// matrix to the full Toffoli matrix and specifying all three qubits in the
/// targets set, or the midway solution using a CNOT matrix, but these
/// solutions may be less efficient depending on whether the simulator can
/// optimize its calculations for controlled gates.
///
/// Simulators are not required to apply the (hidden) global phase component of
/// the gate matrix in the same way it is specified; that is, if the simulator
/// can optimize its calculations by altering the global phase it is allowed
/// to.
///
/// DQCsim checks whether the matrix is unitary using the equivalent of
/// `dqcs_mat_approx_unitary()` with an epsilon value of 1e-6.
///
/// This function returns the handle to the gate, or 0 to indicate failure.
/// The `targets` qubit set, (if specified) the `controls` qubit set, and the
/// matrix are consumed/deleted by this function if and only if it succeeds.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_unitary(
    targets: dqcs_handle_t,
    controls: dqcs_handle_t,
    matrix: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        // Interpret target set.
        resolve!(targets as pending QubitReferenceSet);
        let target_vec: Vec<QubitRef> = {
            let x: &QubitReferenceSet = targets.as_ref().unwrap();
            x.iter().cloned().collect()
        };

        // Interpret control set.
        resolve!(optional controls as pending QubitReferenceSet);
        let control_vec: Vec<QubitRef> = {
            if let Some(controls) = controls.as_ref() {
                let x: &QubitReferenceSet = controls.as_ref().unwrap();
                x.iter().cloned().collect()
            } else {
                vec![]
            }
        };

        // Interpret matrix.
        resolve!(matrix as pending Matrix);
        let matrix_ref: &Matrix = matrix.as_ref().unwrap();

        // Construct the gate.
        let gate = insert(Gate::new_unitary(
            target_vec,
            control_vec,
            matrix_ref.clone(),
        )?);

        // Everything went OK. Now make sure that the target and control set
        // handles are deleted.
        delete!(resolved targets);
        if let Some(mut controls) = controls {
            delete!(resolved controls);
        }
        delete!(resolved matrix);
        Ok(gate)
    })
}

/// Constructs a new measurement gate.
///
/// `measures` must be a handle to a qubit set. `matrix` is an optional matrix
/// handle signifying the measurement basis. If zero, the Z basis is used.
/// Otherwise, it must be a handle to a unitary 2x2 matrix, and the semantics
/// of the measurement are as follows:
///
///  - apply the hermetian of the matrix to each qubit
///  - measure each qubit in the Z basis
///  - apply the matrix to each qubit
///
/// This function returns the handle to the gate, or 0 to indicate failure.
/// The `measures` qubit set and `matrix` handle are consumed/deleted by this
/// function if and only if it succeeds.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_measurement(
    measures: dqcs_handle_t,
    matrix: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        // Interpret measures set.
        resolve!(measures as pending QubitReferenceSet);
        let measure_vec: Vec<QubitRef> = {
            let x: &QubitReferenceSet = measures.as_ref().unwrap();
            x.iter().cloned().collect()
        };

        // Interpret matrix.
        resolve!(optional matrix as pending Matrix);
        let defaulted_matrix = {
            if let Some(matrix) = matrix.as_ref() {
                let matrix: &Matrix = matrix.as_ref().unwrap();
                matrix.clone()
            } else {
                Matrix::new_identity(2)
            }
        };

        // Construct the gate.
        let gate = insert(Gate::new_measurement(measure_vec, defaulted_matrix)?);

        // Everything went OK. Now make sure that the measure set handle is
        // deleted.
        delete!(resolved measures);
        if let Some(mut matrix) = matrix {
            delete!(resolved matrix);
        }
        Ok(gate)
    })
}

/// Constructs a new prep gate.
///
/// `targets` must be a handle to a qubit set. `matrix` is an optional matrix
/// handle signifying the state that the qubits are initialized to. If zero,
/// the qubits are initialized to |0>. Otherwise, it must be a handle to a
/// unitary 2x2 matrix, and the semantics are as follows:
///
///  - initialize each qubit to |0>
///  - apply the matrix to each qubit
///
/// This function returns the handle to the gate, or 0 to indicate failure.
/// The `targets` qubit set and `matrix` handle are consumed/deleted by this
/// function if and only if it succeeds.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_prep(
    targets: dqcs_handle_t,
    matrix: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        // Interpret targets set.
        resolve!(targets as pending QubitReferenceSet);
        let targets_vec: Vec<QubitRef> = {
            let x: &QubitReferenceSet = targets.as_ref().unwrap();
            x.iter().cloned().collect()
        };

        // Interpret matrix.
        resolve!(optional matrix as pending Matrix);
        let defaulted_matrix = {
            if let Some(matrix) = matrix.as_ref() {
                let matrix: &Matrix = matrix.as_ref().unwrap();
                matrix.clone()
            } else {
                Matrix::new_identity(2)
            }
        };

        // Construct the gate.
        let gate = insert(Gate::new_prep(targets_vec, defaulted_matrix)?);

        // Everything went OK. Now make sure that the measure set handle is
        // deleted.
        delete!(resolved targets);
        if let Some(mut matrix) = matrix {
            delete!(resolved matrix);
        }
        Ok(gate)
    })
}

/// Constructs a new custom gate.
///
/// The functionality of custom gates is not specified by DQCsim. Instead, this
/// is left up to the plugins. Of course, for this to work, plugins that are
/// connected to each other must agree on the format used.
///
/// `name` specifies the name of the gate. The name is used to indicate which
/// custom operation is to be applied.
///
/// `targets` optionally specifies the set of target qubits. You may pass 0 or
/// an empty qubit set if you don't need target qubits.
///
/// `controls` optionally specifies the set of control qubits. You may pass 0
/// or an empty qubit set if you don't need control qubits.
///
/// `measures` optionally specifies the set of measured qubits. You may pass 0
/// or an empty qubit set if no qubits are measured. Note that the upstream
/// plugin expects exactly one measurement result for each qubit specified in
/// this set; anything else results in a warning and the measurement result
/// being set to undefined.
///
/// `matrix` optionally specifies a handle to an appropriately sized matrix
/// for the `targets` qubit set.
///
/// In addition to the above data, gate objects implement the `arb` interface
/// to allow user-specified classical information to be attached.
///
/// This function returns the handle to the gate, or 0 to indicate failure.
/// The specified qubit sets are consumed/deleted by this function if and only
/// if it succeeds.
#[no_mangle]
pub extern "C" fn dqcs_gate_new_custom(
    name: *const c_char,
    targets: dqcs_handle_t,
    controls: dqcs_handle_t,
    measures: dqcs_handle_t,
    matrix: dqcs_handle_t,
) -> dqcs_handle_t {
    api_return(0, || {
        // Interpret name.
        let name = receive_str(name)?;

        // Interpret target set.
        resolve!(optional targets as pending QubitReferenceSet);
        let target_vec: Vec<QubitRef> = {
            if let Some(targets) = targets.as_ref() {
                let x: &QubitReferenceSet = targets.as_ref().unwrap();
                x.iter().cloned().collect()
            } else {
                vec![]
            }
        };

        // Interpret control set.
        resolve!(optional controls as pending QubitReferenceSet);
        let control_vec: Vec<QubitRef> = {
            if let Some(controls) = controls.as_ref() {
                let x: &QubitReferenceSet = controls.as_ref().unwrap();
                x.iter().cloned().collect()
            } else {
                vec![]
            }
        };

        // Interpret measurement set.
        resolve!(optional measures as pending QubitReferenceSet);
        let measure_vec: Vec<QubitRef> = {
            if let Some(measures) = measures.as_ref() {
                let x: &QubitReferenceSet = measures.as_ref().unwrap();
                x.iter().cloned().collect()
            } else {
                vec![]
            }
        };

        // Interpret matrix.
        resolve!(optional matrix as pending Matrix);
        let matrix_clone: Option<Matrix> = {
            if let Some(matrix) = matrix.as_ref() {
                let x: &Matrix = matrix.as_ref().unwrap();
                Some(x.clone())
            } else {
                None
            }
        };

        // Construct the gate.
        let gate = insert(Gate::new_custom(
            name,
            target_vec,
            control_vec,
            measure_vec,
            matrix_clone,
            ArbData::default(),
        )?);

        // Everything went OK. Now make sure that the specified qubit set
        // handles are deleted.
        if let Some(mut targets) = targets {
            delete!(resolved targets);
        }
        if let Some(mut controls) = controls {
            delete!(resolved controls);
        }
        if let Some(mut measures) = measures {
            delete!(resolved measures);
        }
        if let Some(mut matrix) = matrix {
            delete!(resolved matrix);
        }
        Ok(gate)
    })
}

/// Returns the gate type of the given gate.
///
/// Returns DQCS_GATE_TYPE_INVALID if the gate handle is invalid.
#[no_mangle]
pub extern "C" fn dqcs_gate_type(gate: dqcs_handle_t) -> dqcs_gate_type_t {
    api_return(dqcs_gate_type_t::DQCS_GATE_TYPE_INVALID, || {
        resolve!(gate as &Gate);
        Ok(gate.get_type().into())
    })
}

/// Returns whether the specified gate has a name.
#[no_mangle]
pub extern "C" fn dqcs_gate_has_name(gate: dqcs_handle_t) -> dqcs_bool_return_t {
    api_return_bool(|| {
        resolve!(gate as &Gate);
        Ok(gate.get_name().is_some())
    })
}

/// Returns the name of a custom gate.
///
/// This function fails if the gate is not a custom gate. Query
/// `dqcs_gate_has_name()` to disambiguate between a non-custom gate and a
/// different error.
///
/// On success, this **returns a newly allocated string containing the gate
/// name. Free it with `free()` when you're done with it to avoid memory
/// leaks.** On failure, this returns `NULL`.
#[no_mangle]
pub extern "C" fn dqcs_gate_name(gate: dqcs_handle_t) -> *mut c_char {
    api_return_string(|| {
        resolve!(gate as &Gate);
        Ok(gate
            .get_name()
            .ok_or_else(oe_inv_arg(
                "gate is not custom and thus does not have a name",
            ))?
            .to_string())
    })
}

/// Returns whether the specified gate has target qubits.
#[no_mangle]
pub extern "C" fn dqcs_gate_has_targets(gate: dqcs_handle_t) -> dqcs_bool_return_t {
    api_return_bool(|| {
        resolve!(gate as &Gate);
        Ok(!gate.get_targets().is_empty())
    })
}

/// Returns a handle to a new qubit reference set containing the qubits
/// targeted by this gate.
#[no_mangle]
pub extern "C" fn dqcs_gate_targets(gate: dqcs_handle_t) -> dqcs_handle_t {
    api_return(0, || {
        resolve!(gate as &Gate);
        let targets = gate.get_targets();
        let targets: QubitReferenceSet = targets.iter().cloned().collect();
        Ok(insert(targets))
    })
}

/// Returns whether the specified gate has control qubits.
#[no_mangle]
pub extern "C" fn dqcs_gate_has_controls(gate: dqcs_handle_t) -> dqcs_bool_return_t {
    api_return_bool(|| {
        resolve!(gate as &Gate);
        Ok(!gate.get_controls().is_empty())
    })
}

/// Returns a handle to a new qubit reference set containing the qubits
/// that control this gate.
#[no_mangle]
pub extern "C" fn dqcs_gate_controls(gate: dqcs_handle_t) -> dqcs_handle_t {
    api_return(0, || {
        resolve!(gate as &Gate);
        let controls = gate.get_controls();
        let controls: QubitReferenceSet = controls.iter().cloned().collect();
        Ok(insert(controls))
    })
}

/// Returns whether the specified gate measures any qubits.
#[no_mangle]
pub extern "C" fn dqcs_gate_has_measures(gate: dqcs_handle_t) -> dqcs_bool_return_t {
    api_return_bool(|| {
        resolve!(gate as &Gate);
        Ok(!gate.get_measures().is_empty())
    })
}

/// Returns a handle to a new qubit reference set containing the qubits
/// measured by this gate.
#[no_mangle]
pub extern "C" fn dqcs_gate_measures(gate: dqcs_handle_t) -> dqcs_handle_t {
    api_return(0, || {
        resolve!(gate as &Gate);
        let measures = gate.get_measures();
        let measures: QubitReferenceSet = measures.iter().cloned().collect();
        Ok(insert(measures))
    })
}

/// Returns whether a unitary matrix is associated with this gate.
#[no_mangle]
pub extern "C" fn dqcs_gate_has_matrix(gate: dqcs_handle_t) -> dqcs_bool_return_t {
    api_return_bool(|| {
        resolve!(gate as &Gate);
        Ok(gate.get_matrix().is_some())
    })
}

/// Returns a copy of the unitary matrix associated with this gate, if one
/// exists.
///
/// If this function succeeds, a new matrix handle is returned. If it fails,
/// 0 is returned.
#[no_mangle]
pub extern "C" fn dqcs_gate_matrix(gate: dqcs_handle_t) -> dqcs_handle_t {
    api_return(0, || {
        resolve!(gate as &Gate);
        Ok(insert(
            gate.get_matrix()
                .ok_or_else(oe_inv_arg("no matrix associated with gate"))?
                .clone(),
        ))
    })
}

/// Utility function that detects control qubits in the `targets` list of the
/// gate by means of the gate matrix, and reduces them into `controls` qubits.
///>
///> This function borrows a handle to any gate with a matrix, and returns an
///> equivalent copy of said gate with any control qubits in the `targets` set
///> moved to the `controls` set. The associated gate matrix is accordingly
///> reduced in size. The control qubits are added at the end of the `controls`
///> set in the same order they appeared in the `targets` qubit set.
///>
///> `epsilon` specifies the maximum element-wise deviation from the identity
///> matrix for the relevant array elements for a qubit to be considered a
///> control qubit. Note that if this is greater than zero, the resulting gate
///> may not be exactly equivalent. If `ignore_gphase` is set, any global phase
///> in the matrix is ignored, but the global phase of the non-control submatrix
///> is not changed.
///>
///> This function returns a new gate handle with the modified gate, or a copy
///> of the input gate if the matrix could not be reduced. If the input gate
///> does not have a matrix (measurement gate, or custom gate without matrix) an
///> error is returned instead.
#[no_mangle]
pub extern "C" fn dqcs_gate_reduce_control(
    gate: dqcs_handle_t,
    epsilon: c_double,
    ignore_gphase: bool,
) -> dqcs_handle_t {
    api_return(0, || {
        resolve!(gate as &Gate);
        if gate.get_matrix().is_none() {
            inv_arg("no matrix associated with gate")
        } else {
            Ok(insert(gate.with_gate_controls(epsilon, ignore_gphase)))
        }
    })
}

/// Utility function that expands a gate matrix to account for all control
/// qubits.
///>
///> This function borrows a handle to any gate with a matrix, and returns an
///> equivalent copy of said gate with any control qubits in the `controls` set
///> moved to the `targets` set. The associated gate matrix is extended
///> accordingly. The control qubits are added at the front of the `targets`
///> set in the same order they appeared in the `controls` qubit set.
///>
///> This function returns a new gate handle with the modified gate, or a copy
///> of the input gate if the matrix could not be reduced. If the input gate
///> does not have a matrix (measurement gate, or custom gate without matrix) an
///> error is returned instead.
#[no_mangle]
pub extern "C" fn dqcs_gate_expand_control(gate: dqcs_handle_t) -> dqcs_handle_t {
    api_return(0, || {
        resolve!(gate as &Gate);
        if gate.get_matrix().is_none() {
            inv_arg("no matrix associated with gate")
        } else {
            Ok(insert(gate.with_matrix_controls()))
        }
    })
}