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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
use std::fmt;
use std::rc::Rc;

use num::complex::Complex;

use crate::pipeline::*;
use crate::state_ops::*;
use crate::types::Precision;

/// Possible relations to a parent qubit
pub enum Parent {
    Owned(Vec<Qubit>, Option<StateModifier>),
    Shared(Rc<Qubit>),
}

/// A qubit object, possible representing multiple physical qubit indices.
pub struct Qubit {
    pub indices: Vec<u64>,
    pub parent: Option<Parent>,
    pub id: u64,
}

impl Qubit {
    fn new(id: u64, indices: Vec<u64>) -> Result<Qubit, &'static str> {
        if indices.is_empty() {
            Err("Qubit must have nonzero number of indices.")
        } else {
            Ok(Qubit {
                indices,
                parent: None,
                id,
            })
        }
    }

    /// Create a handle for feeding values.
    pub fn handle(&self) -> QubitHandle {
        QubitHandle {
            indices: self.indices.clone(),
        }
    }

    /// Merge qubits to for a new qubit object.
    pub fn merge_with_modifier(
        id: u64,
        qubits: Vec<Qubit>,
        modifier: Option<StateModifier>,
    ) -> Qubit {
        let mut all_indices = Vec::new();

        for q in qubits.iter() {
            all_indices.extend(q.indices.iter());
        }
        all_indices.sort();

        Qubit {
            indices: all_indices,
            parent: Some(Parent::Owned(qubits, modifier)),
            id,
        }
    }

    /// Split the relative indices out of `q` into its own qubit, remaining live in second qubit.
    pub fn split(
        ida: u64,
        idb: u64,
        q: Qubit,
        indices: Vec<u64>,
    ) -> Result<(Qubit, Qubit), &'static str> {
        for indx in &indices {
            if *indx > (q.indices.len() as u64) {
                return Err("All indices for splitting must be below q.n");
            }
        }
        if indices.len() == q.indices.len() {
            Err("Indices must leave at least one index.")
        } else if indices.is_empty() {
            Err("Indices must contain at least one index.")
        } else {
            let selected_indices: Vec<u64> =
                indices.into_iter().map(|i| q.indices[i as usize]).collect();
            Self::split_absolute(ida, idb, q, selected_indices)
        }
    }

    /// Split a qubit in two, with one having the indices in `selected_indices`
    pub fn split_absolute(
        ida: u64,
        idb: u64,
        q: Qubit,
        selected_indices: Vec<u64>,
    ) -> Result<(Qubit, Qubit), &'static str> {
        if selected_indices.len() == q.indices.len() {
            return Err("Cannot split out all indices into own qubit.");
        } else if selected_indices.is_empty() {
            return Err("Must provide indices to split.");
        }
        for indx in &selected_indices {
            if !q.indices.contains(indx) {
                return Err("All indices must exist in qubit to be split.");
            }
        }

        let remaining = q
            .indices
            .clone()
            .into_iter()
            .filter(|x| !selected_indices.contains(x))
            .collect();
        let shared_parent = Rc::new(q);

        Ok((
            Qubit {
                indices: selected_indices,
                parent: Some(Parent::Shared(shared_parent.clone())),
                id: ida,
            },
            Qubit {
                indices: remaining,
                parent: Some(Parent::Shared(shared_parent.clone())),
                id: idb,
            },
        ))
    }

    /// Get number of qubits in this Qubit object
    pub fn n(&self) -> u64 {
        self.indices.len() as u64
    }
}

impl std::cmp::Eq for Qubit {}

impl std::cmp::PartialEq for Qubit {
    fn eq(&self, other: &Qubit) -> bool {
        self.id == other.id
    }
}

impl std::cmp::Ord for Qubit {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.id.cmp(&other.id)
    }
}

impl std::cmp::PartialOrd for Qubit {
    fn partial_cmp(&self, other: &Qubit) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl fmt::Debug for Qubit {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let int_strings = self
            .indices
            .iter()
            .map(|x| x.clone().to_string())
            .collect::<Vec<String>>();

        write!(
            f,
            "Qubit[{}][{}]",
            self.id.to_string(),
            int_strings.join(", ")
        )
    }
}

pub struct QubitHandle {
    indices: Vec<u64>,
}

impl QubitHandle {
    pub fn make_init_from_index<P: Precision>(
        &self,
        index: u64,
    ) -> Result<QubitInitialState<P>, &'static str> {
        if index < 1 << self.indices.len() as u64 {
            Ok((self.indices.clone(), InitialState::Index(index)))
        } else {
            Err("Index too large for QubitHandle")
        }
    }
    pub fn make_init_from_state<P: Precision>(
        &self,
        state: Vec<Complex<P>>,
    ) -> Result<QubitInitialState<P>, &'static str> {
        if state.len() == 1 << self.indices.len() {
            Ok((self.indices.clone(), InitialState::FullState(state)))
        } else {
            Err("State not correct size for QubitHandle (must be 2^n)")
        }
    }

    pub fn init_index<P: Precision>(&self, index: u64) -> QubitInitialState<P> {
        self.make_init_from_index(index).unwrap()
    }

    pub fn init_state<P: Precision>(&self, state: Vec<Complex<P>>) -> QubitInitialState<P> {
        self.make_init_from_state(state).unwrap()
    }
}

/// A builder which supports non-unitary operations
pub trait NonUnitaryBuilder {
    /// Add a measure op to the pipeline for `q` and return a reference which can
    /// later be used to access the measured value from the results of `pipeline::run`.
    fn measure(&mut self, q: Qubit) -> (Qubit, u64);
}

/// A builder which support unitary operations
pub trait UnitaryBuilder {
    // Things like X, Y, Z, NOT, H, SWAP, ... go here

    /// Build a builder which uses `q` as context.
    fn with_context(&mut self, q: Qubit) -> ConditionalContextBuilder;

    /// Build a generic matrix op, apply to `q`, if `q` is multiple indices and
    /// mat is 2x2, apply to each index, otherwise returns an error if the matrix is not the correct
    /// size for the number of indices in `q` (mat.len() == 2^(2n)).
    fn mat(&mut self, name: &str, q: Qubit, mat: Vec<Complex<f64>>) -> Result<Qubit, &'static str>;

    /// Build a matrix op from real numbers, apply to `q`, if `q` is multiple indices and
    /// mat is 2x2, apply to each index, otherwise returns an error if the matrix is not the correct
    /// size for the number of indices in `q` (mat.len() == 2^(2n)).
    fn real_mat(&mut self, name: &str, q: Qubit, mat: &[f64]) -> Result<Qubit, &'static str> {
        self.mat(name, q, from_reals(mat))
    }

    /// Build a sparse matrix op, apply to `q`, if `q` is multiple indices and
    /// mat is 2x2, apply to each index, otherwise returns an error if the matrix is not the correct
    /// size for the number of indices in `q` (mat.len() == 2^n).
    fn sparse_mat(
        &mut self,
        name: &str,
        q: Qubit,
        mat: Vec<Vec<(u64, Complex<f64>)>>,
    ) -> Result<Qubit, &'static str>;

    /// Build a sparse matrix op from real numbers, apply to `q`, if `q` is multiple indices and
    /// mat is 2x2, apply to each index, otherwise returns an error if the matrix is not the correct
    /// size for the number of indices in `q` (mat.len() == 2^n).
    fn real_sparse_mat(
        &mut self,
        name: &str,
        q: Qubit,
        mat: &[Vec<(u64, f64)>],
    ) -> Result<Qubit, &'static str> {
        let mat = mat
            .iter()
            .map(|v| {
                v.iter()
                    .cloned()
                    .map(|(c, r)| (c, Complex { re: r, im: 0.0 }))
                    .collect()
            })
            .collect();
        self.sparse_mat(name, q, mat)
    }

    /// Apply NOT to `q`, if `q` is multiple indices, apply to each
    fn not(&mut self, q: Qubit) -> Qubit {
        self.real_mat("not", q, &[0.0, 1.0, 1.0, 0.0]).unwrap()
    }

    /// Apply X to `q`, if `q` is multiple indices, apply to each
    fn x(&mut self, q: Qubit) -> Qubit {
        self.real_mat("X", q, &[0.0, 1.0, 1.0, 0.0]).unwrap()
    }

    /// Apply Y to `q`, if `q` is multiple indices, apply to each
    fn y(&mut self, q: Qubit) -> Qubit {
        self.mat(
            "Y",
            q,
            from_tuples(&[(0.0, 0.0), (0.0, -1.0), (0.0, 0.0), (0.0, 1.0)]),
        )
        .unwrap()
    }

    /// Apply Z to `q`, if `q` is multiple indices, apply to each
    fn z(&mut self, q: Qubit) -> Qubit {
        self.real_mat("Z", q, &[1.0, 0.0, 0.0, -1.0]).unwrap()
    }

    /// Apply H to `q`, if `q` is multiple indices, apply to each
    fn hadamard(&mut self, q: Qubit) -> Qubit {
        let inv_sqrt = 1.0f64 / 2.0f64.sqrt();
        self.real_mat("H", q, &[inv_sqrt, inv_sqrt, inv_sqrt, -inv_sqrt])
            .unwrap()
    }

    /// Apply SWAP to `qa` and `qb`
    fn swap(&mut self, qa: Qubit, qb: Qubit) -> Result<(Qubit, Qubit), &'static str> {
        let op = self.make_swap_op(&qa, &qb)?;
        let qa_indices = qa.indices.clone();

        let name = String::from("swap");
        let q = self.merge_with_op(vec![qa, qb], Some((name, op)));
        self.split_absolute(q, qa_indices)
    }

    /// Make an operation from the boxed function `f`. This maps c|`q_in`>|`q_out`> to
    /// c*e^i`theta`|`q_in`>|`q_out` ^ `indx`> where `indx` and `theta` are the outputs from the
    /// function `f(x) = (indx, theta)`
    fn apply_function(
        &mut self,
        q_in: Qubit,
        q_out: Qubit,
        f: Box<Fn(u64) -> (u64, f64) + Send + Sync>,
    ) -> Result<(Qubit, Qubit), &'static str>;

    /// Merge the qubits in `qs` into a single qubit.
    fn merge(&mut self, qs: Vec<Qubit>) -> Qubit {
        self.merge_with_op(qs, None)
    }

    /// Split the qubit `q` into two qubits, one with relative `indices` and one with the remaining.
    fn split(&mut self, q: Qubit, indices: Vec<u64>) -> Result<(Qubit, Qubit), &'static str> {
        for indx in &indices {
            if *indx > (q.indices.len() as u64) {
                return Err("All indices for splitting must be below q.n");
            }
        }
        if indices.is_empty() {
            Err("Indices must contain at least one index.")
        } else if indices.len() == q.indices.len() {
            Err("Indices must leave at least one index.")
        } else {
            let selected_indices: Vec<u64> =
                indices.into_iter().map(|i| q.indices[i as usize]).collect();
            self.split_absolute(q, selected_indices)
        }
    }

    /// Split the qubit `q` into two qubits, one with `selected_indices` and one with the remaining.
    fn split_absolute(
        &mut self,
        q: Qubit,
        selected_indices: Vec<u64>,
    ) -> Result<(Qubit, Qubit), &'static str>;

    /// Split the qubit into many qubits, each with the given set of indices.
    fn split_absolute_many(
        &mut self,
        q: Qubit,
        index_groups: Vec<Vec<u64>>,
    ) -> Result<(Vec<Qubit>, Qubit), &'static str> {
        Ok(index_groups
            .into_iter()
            .fold((vec![], q), |(mut qs, q), indices| {
                let (hq, tq) = self.split_absolute(q, indices).unwrap();
                qs.push(hq);
                (qs, tq)
            }))
    }

    /// Split `q` into a single qubit for each index.
    fn split_all(&mut self, q: Qubit) -> Vec<Qubit> {
        if q.indices.len() == 1 {
            vec![q]
        } else {
            let mut indices: Vec<Vec<u64>> = q.indices.iter().cloned().map(|i| vec![i]).collect();
            indices.pop();
            // Cannot fail since all indices are from q.
            let (mut qs, q) = self.split_absolute_many(q, indices).unwrap();
            qs.push(q);
            qs
        }
    }

    /// Build a generic matrix op.
    fn make_mat_op(&self, q: &Qubit, data: Vec<Complex<f64>>) -> Result<QubitOp, &'static str> {
        make_matrix_op(q.indices.clone(), data)
    }

    /// Build a sparse matrix op
    fn make_sparse_mat_op(
        &self,
        q: &Qubit,
        data: Vec<Vec<(u64, Complex<f64>)>>,
    ) -> Result<QubitOp, &'static str> {
        make_sparse_matrix_op(q.indices.clone(), data, false)
    }

    /// Build a swap op. qa and qb must have the same number of indices.
    fn make_swap_op(&self, qa: &Qubit, qb: &Qubit) -> Result<QubitOp, &'static str> {
        make_swap_op(qa.indices.clone(), qb.indices.clone())
    }

    /// Make a function op. f must be boxed so that this function doesn't need to be parameterized.
    fn make_function_op(
        &self,
        q_in: &Qubit,
        q_out: &Qubit,
        f: Box<Fn(u64) -> (u64, f64) + Send + Sync>,
    ) -> Result<QubitOp, &'static str> {
        make_function_op(q_in.indices.clone(), q_out.indices.clone(), f)
    }

    /// Merge qubits using a generic state processing function.
    fn merge_with_op(&mut self, qs: Vec<Qubit>, named_operator: Option<(String, QubitOp)>)
        -> Qubit;

    /// Measure all qubit states and probabilities, does not edit state (thus Unitary). Returns
    /// qubit and handle.
    fn stochastic_measure(&mut self, q: Qubit) -> (Qubit, u64);
}

/// Helper function for Boxing static functions and applying using the given UnitaryBuilder.
pub fn apply_function<F: 'static + Fn(u64) -> (u64, f64) + Send + Sync>(
    b: &mut UnitaryBuilder,
    q_in: Qubit,
    q_out: Qubit,
    f: F,
) -> Result<(Qubit, Qubit), &'static str> {
    b.apply_function(q_in, q_out, Box::new(f))
}

/// A basic builder for unitary and non-unitary ops.
#[derive(Default)]
pub struct OpBuilder {
    qubit_index: u64,
    op_id: u64,
}

impl OpBuilder {
    /// Build a new OpBuilder
    pub fn new() -> OpBuilder {
        OpBuilder::default()
    }

    /// Build a new qubit with `n` indices
    pub fn qubit(&mut self, n: u64) -> Result<Qubit, &'static str> {
        if n == 0 {
            Err("Qubit n must be greater than 0.")
        } else {
            let base_index = self.qubit_index;
            self.qubit_index += n;

            Qubit::new(self.get_op_id(), (base_index..self.qubit_index).collect())
        }
    }

    /// If you just plan to call unwrap this is cleaner.
    pub fn q(&mut self, n: u64) -> Qubit {
        self.qubit(n).unwrap()
    }

    /// Build a new qubit with `n` indices, return it plus a handle which can be
    /// used for feeding in an initial state.
    pub fn qubit_and_handle(&mut self, n: u64) -> Result<(Qubit, QubitHandle), &'static str> {
        let q = self.qubit(n)?;
        let h = q.handle();
        Ok((q, h))
    }

    fn get_op_id(&mut self) -> u64 {
        let tmp = self.op_id;
        self.op_id += 1;
        tmp
    }
}

impl NonUnitaryBuilder for OpBuilder {
    fn measure(&mut self, q: Qubit) -> (Qubit, u64) {
        let id = self.get_op_id();
        let modifier =
            StateModifier::new_measurement(String::from("measure"), id, q.indices.clone());
        let modifier = Some(modifier);
        let q = Qubit::merge_with_modifier(id, vec![q], modifier);
        (q, id)
    }
}

impl UnitaryBuilder for OpBuilder {
    fn with_context(&mut self, q: Qubit) -> ConditionalContextBuilder {
        ConditionalContextBuilder {
            parent_builder: self,
            conditioned_qubit: Some(q),
        }
    }

    fn mat(&mut self, name: &str, q: Qubit, mat: Vec<Complex<f64>>) -> Result<Qubit, &'static str> {
        // Special case for broadcasting ops
        if q.indices.len() > 1 && mat.len() == (2 * 2) {
            let qs = self.split_all(q);
            let qs = qs
                .into_iter()
                .map(|q| self.mat(name, q, mat.clone()).unwrap())
                .collect();
            Ok(self.merge_with_op(qs, None))
        } else {
            let op = self.make_mat_op(&q, mat)?;
            let name = String::from(name);
            Ok(self.merge_with_op(vec![q], Some((name, op))))
        }
    }

    fn sparse_mat(
        &mut self,
        name: &str,
        q: Qubit,
        mat: Vec<Vec<(u64, Complex<f64>)>>,
    ) -> Result<Qubit, &'static str> {
        // Special case for broadcasting ops
        if q.indices.len() > 1 && mat.len() == (2 * 2) {
            let qs = self.split_all(q);
            let qs = qs
                .into_iter()
                .map(|q| self.sparse_mat(name, q, mat.clone()).unwrap())
                .collect();
            Ok(self.merge_with_op(qs, None))
        } else {
            let op = self.make_sparse_mat_op(&q, mat)?;
            let name = String::from(name);
            Ok(self.merge_with_op(vec![q], Some((name, op))))
        }
    }

    fn apply_function(
        &mut self,
        q_in: Qubit,
        q_out: Qubit,
        f: Box<Fn(u64) -> (u64, f64) + Send + Sync>,
    ) -> Result<(Qubit, Qubit), &'static str> {
        let op = self.make_function_op(&q_in, &q_out, f)?;
        let in_indices = q_in.indices.clone();
        let name = String::from("f");
        let q = self.merge_with_op(vec![q_in, q_out], Some((name, op)));
        self.split_absolute(q, in_indices)
    }

    fn split_absolute(
        &mut self,
        q: Qubit,
        selected_indices: Vec<u64>,
    ) -> Result<(Qubit, Qubit), &'static str> {
        Qubit::split_absolute(self.get_op_id(), self.get_op_id(), q, selected_indices)
    }

    fn merge_with_op(
        &mut self,
        qs: Vec<Qubit>,
        named_operator: Option<(String, QubitOp)>,
    ) -> Qubit {
        let modifier = named_operator.map(|(name, op)| StateModifier::new_unitary(name, op));
        Qubit::merge_with_modifier(self.get_op_id(), qs, modifier)
    }

    fn stochastic_measure(&mut self, q: Qubit) -> (Qubit, u64) {
        let id = self.get_op_id();
        let modifier = StateModifier::new_stochastic_measurement(
            String::from("stochastic"),
            id,
            q.indices.clone(),
        );
        let modifier = Some(modifier);
        let q = Qubit::merge_with_modifier(id, vec![q], modifier);
        (q, id)
    }
}

/// An op builder which depends on the value of a given qubit (COPs)
pub struct ConditionalContextBuilder<'a> {
    parent_builder: &'a mut UnitaryBuilder,
    conditioned_qubit: Option<Qubit>,
}

impl<'a> ConditionalContextBuilder<'a> {
    /// Release the qubit used to build this builder
    pub fn release_qubit(self: Self) -> Qubit {
        match self.conditioned_qubit {
            Some(q) => q,
            None => panic!("Conditional context builder failed to populate qubit."),
        }
    }

    fn get_conditional_qubit(&mut self) -> Qubit {
        self.conditioned_qubit.take().unwrap()
    }

    fn set_conditional_qubit(&mut self, cq: Qubit) {
        self.conditioned_qubit = Some(cq);
    }
}

impl<'a> UnitaryBuilder for ConditionalContextBuilder<'a> {
    fn with_context(&mut self, q: Qubit) -> ConditionalContextBuilder {
        ConditionalContextBuilder {
            parent_builder: self,
            conditioned_qubit: Some(q),
        }
    }

    fn mat(&mut self, name: &str, q: Qubit, mat: Vec<Complex<f64>>) -> Result<Qubit, &'static str> {
        // Special case for applying mat to each qubit in collection.
        if q.indices.len() > 1 && mat.len() == (2 * 2) {
            let qs = self.split_all(q);
            let qs = qs
                .into_iter()
                .map(|q| self.mat(name, q, mat.clone()).unwrap())
                .collect();
            Ok(self.merge_with_op(qs, None))
        } else {
            let op = self.make_mat_op(&q, mat)?;
            let cq = self.get_conditional_qubit();
            let cq_indices = cq.indices.clone();
            let name = format!("C({})", name);
            let q = self.merge_with_op(vec![cq, q], Some((name, op)));
            let (cq, q) = self.split_absolute(q, cq_indices).unwrap();

            self.set_conditional_qubit(cq);
            Ok(q)
        }
    }

    fn sparse_mat(
        &mut self,
        name: &str,
        q: Qubit,
        mat: Vec<Vec<(u64, Complex<f64>)>>,
    ) -> Result<Qubit, &'static str> {
        // Special case for applying mat to each qubit in collection.
        if q.indices.len() > 1 && mat.len() == (2 * 2) {
            let qs = self.split_all(q);
            let qs = qs
                .into_iter()
                .map(|q| self.sparse_mat(name, q, mat.clone()).unwrap())
                .collect();
            Ok(self.merge_with_op(qs, None))
        } else {
            let op = self.make_sparse_mat_op(&q, mat)?;
            let cq = self.get_conditional_qubit();
            let cq_indices = cq.indices.clone();
            let name = format!("C({})", name);
            let q = self.merge_with_op(vec![cq, q], Some((name, op)));
            let (cq, q) = self.split_absolute(q, cq_indices).unwrap();

            self.set_conditional_qubit(cq);
            Ok(q)
        }
    }

    fn swap(&mut self, qa: Qubit, qb: Qubit) -> Result<(Qubit, Qubit), &'static str> {
        let op = self.make_swap_op(&qa, &qb)?;
        let cq = self.get_conditional_qubit();
        let cq_indices = cq.indices.clone();
        let qa_indices = qa.indices.clone();
        let name = String::from("C(swap)");
        let q = self.merge_with_op(vec![cq, qa, qb], Some((name, op)));
        let (cq, q) = self.split_absolute(q, cq_indices).unwrap();
        let (qa, qb) = self.split_absolute(q, qa_indices).unwrap();

        self.set_conditional_qubit(cq);
        Ok((qa, qb))
    }

    fn apply_function(
        &mut self,
        q_in: Qubit,
        q_out: Qubit,
        f: Box<Fn(u64) -> (u64, f64) + Send + Sync>,
    ) -> Result<(Qubit, Qubit), &'static str> {
        let op = self.make_function_op(&q_in, &q_out, f)?;
        let cq = self.get_conditional_qubit();

        let cq_indices = cq.indices.clone();
        let in_indices = q_in.indices.clone();
        let name = String::from("C(f)");
        let q = self.merge_with_op(vec![cq, q_in, q_out], Some((name, op)));
        let (cq, q) = self.split_absolute(q, cq_indices).unwrap();
        let (q_in, q_out) = self.split_absolute(q, in_indices).unwrap();

        self.set_conditional_qubit(cq);
        Ok((q_in, q_out))
    }

    fn split_absolute(
        &mut self,
        q: Qubit,
        selected_indices: Vec<u64>,
    ) -> Result<(Qubit, Qubit), &'static str> {
        self.parent_builder.split_absolute(q, selected_indices)
    }

    fn make_mat_op(&self, q: &Qubit, data: Vec<Complex<f64>>) -> Result<QubitOp, &'static str> {
        match &self.conditioned_qubit {
            Some(cq) => make_control_op(
                cq.indices.clone(),
                self.parent_builder.make_mat_op(q, data)?,
            ),
            None => panic!("Conditional context builder failed to populate qubit."),
        }
    }

    fn make_sparse_mat_op(
        &self,
        q: &Qubit,
        data: Vec<Vec<(u64, Complex<f64>)>>,
    ) -> Result<QubitOp, &'static str> {
        match &self.conditioned_qubit {
            Some(cq) => make_control_op(
                cq.indices.clone(),
                self.parent_builder.make_sparse_mat_op(q, data)?,
            ),
            None => panic!("Conditional context builder failed to populate qubit."),
        }
    }

    fn make_swap_op(&self, qa: &Qubit, qb: &Qubit) -> Result<QubitOp, &'static str> {
        match &self.conditioned_qubit {
            Some(cq) => {
                let op = self.parent_builder.make_swap_op(qa, qb)?;
                make_control_op(cq.indices.clone(), op)
            }
            None => panic!("Conditional context builder failed to populate qubit."),
        }
    }

    fn make_function_op(
        &self,
        q_in: &Qubit,
        q_out: &Qubit,
        f: Box<Fn(u64) -> (u64, f64) + Send + Sync>,
    ) -> Result<QubitOp, &'static str> {
        match &self.conditioned_qubit {
            Some(cq) => {
                let op = self.parent_builder.make_function_op(q_in, q_out, f)?;
                make_control_op(cq.indices.clone(), op)
            }
            None => panic!("Conditional context builder failed to populate qubit."),
        }
    }

    fn merge_with_op(
        &mut self,
        qs: Vec<Qubit>,
        named_operator: Option<(String, QubitOp)>,
    ) -> Qubit {
        self.parent_builder.merge_with_op(qs, named_operator)
    }

    fn stochastic_measure(&mut self, q: Qubit) -> (Qubit, u64) {
        self.parent_builder.stochastic_measure(q)
    }
}