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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
// Copyright 2019 Q1t BV
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Structure to build up contents of LaTeX export
///
/// Struct `LatexExportState` is used to build up the matrix containing the
/// Qcircuit code for the export of a `Circuit` to LaTeX.
pub struct LatexExportState
{
    // Variables relating to the circuit

    /// The number of quantum bits in the circuit.
    nr_qbits: usize,
    /// The number of classical bits in the circuit.
    nr_cbits: usize,

    // Settings for output

    /// If `true` (the default), add initialization of the bits to the circuit.
    add_init: bool,
    /// If `true` (the default), composite gates are expanded into primitive
    /// gates in the export.
    expand_composite: bool,

    // Runtime variables

    /// Matrix containing LaTex code for each individual gate. Every row in
    /// the matrix corresponds to a column in the exported circuit.
    matrix: Vec<Vec<Option<String>>>,
    /// Vector containing which fields in the last row are currently occupied.
    /// Unoccupied fields can be used, if a gate operates on an occupied field,
    /// a new row must be added.
    in_use: Vec<bool>,
    /// Whether the gate currently being exported is being controlled or not.
    /// This has an impact on the representation of certain gates, e.g. the `X`
    /// gate which is normally represented as a boxed X character, but is
    /// represented by a circled plus (⊕) when it is controlled.
    controlled: bool,
    /// When the state is currently in the process of drawing an operation
    /// operating on a range of bits with possible gaps, this holds the minimum
    /// and maximum bit index of said range.
    reserved_ranges: Vec<(usize, usize)>,
    /// start and end row, and nr of iterations, of static loops.
    loops: Vec<(usize, usize, usize)>,
    /// Start index and nr of iterations of currently unfinished static loops.
    /// Vector because noops may be nested.
    open_loops: Vec<(usize, usize)>
}

impl LatexExportState
{
    /// Create a new LatexExportState
    ///
    /// Create a new `LatexExportState`, for a circuit with `nr_qbits` quantum
    /// bits and `nr_cbits` classical bits.
    pub fn new(nr_qbits: usize, nr_cbits: usize) -> Self
    {
        LatexExportState
        {
            nr_qbits: nr_qbits,
            nr_cbits: nr_cbits,
            add_init: true,
            expand_composite: true,
            matrix: vec![],
            in_use: vec![true; nr_qbits + nr_cbits],
            controlled: false,
            reserved_ranges: vec![],
            loops: vec![],
            open_loops: vec![]
        }
    }

    /// The total number of bits (quantum or classical) in the circuit.
    fn total_nr_bits(&self) -> usize
    {
        self.nr_qbits + self.nr_cbits
    }

    /// Add a new column.
    ///
    /// Add a new column to the export. Used when a new gate operates on a bit
    /// that is already in use.
    fn add_column(&mut self)
    {
        let nr_bits = self.total_nr_bits();
        self.matrix.push(vec![None; nr_bits]);
        self.in_use.clear();
        self.in_use.resize(nr_bits, false);
    }

    fn get_bit_indices(&self, qbits: &[usize], cbits: Option<&[usize]>)
        -> crate::error::Result<Vec<usize>>
    {
        if let Some(&bit) = qbits.iter().find(|&&b| b >= self.nr_qbits)
        {
            return Err(crate::error::Error::InvalidQBit(bit));
        }

        let mut bits = qbits.to_vec();
        if let Some(cbs) = cbits
        {
            if let Some(&bit) = cbs.iter().find(|&&b| b >= self.nr_cbits)
            {
                return Err(crate::error::Error::InvalidCBit(bit));
            }
            bits.extend(cbs.iter().map(|&b| self.nr_qbits + b));
        }

        Ok(bits)
    }

    /// Ensure that fields are free.
    ///
    /// Ensure that the fields for the bits in `qbits` and (optionally) `cbits`
    /// are currently unoccupied. If not, add a new column to the export.
    pub fn reserve(&mut self, qbits: &[usize], cbits: Option<&[usize]>)
        -> crate::error::Result<()>
    {
        let bits = self.get_bit_indices(qbits, cbits)?;
        if bits.iter().any(|&b| self.in_use[b])
        {
            self.add_column();
        }

        Ok(())
    }

    /// Ensure all fields are free.
    ///
    /// Ensure that the last column is currently fully empty. If not, add a new
    /// column to the export.
    fn reserve_all(&mut self)
    {
        if self.in_use.contains(&true)
        {
            self.add_column();
        }
    }

    /// Ensure that fields are free.
    ///
    /// Ensure that the fields for the bits in `qbits` and (optionally) `cbits`,
    /// as well as all field in the range between the minimum and maximum bit,
    /// are currently unoccupied. If not, add a new column to the export.
    pub fn start_range_op(&mut self, qbits: &[usize], cbits: Option<&[usize]>)
        -> crate::error::Result<()>
    {
        let bits = self.get_bit_indices(qbits, cbits)?;
        if let Some(&first) = bits.iter().min()
        {
            let last = *bits.iter().max().unwrap();

            if self.reserved_ranges.is_empty()
            {
                if self.in_use[first..=last].contains(&true)
                {
                    self.add_column();
                }
                self.reserved_ranges.push((first, last));
            }
            else
            {
                let &(outer_first, outer_last) = self.reserved_ranges.last().unwrap();
                if outer_first <= first && outer_last >= last
                {
                    self.reserved_ranges.push((first, last));
                }
                else
                {
                    return Err(crate::error::Error::from(crate::error::ExportError::RangeAlreadyOpen));
                }
            }

        }

        Ok(())
    }

    /// Mark fields as in use.
    ///
    /// Mark the fields in the fields in the range last opened by
    /// `start_range_op()` as in use, and close the range.
    pub fn end_range_op(&mut self)
    {
        if let Some((first, last)) = self.reserved_ranges.pop()
        {
            for bit in first..=last
            {
                self.in_use[bit] = true;
            }
        }
    }

    /// Set the contents of a field
    ///
    /// Set the contents of the field corresponding to bit `bit` to the LaTeX
    /// code in `contents`.
    pub fn set_field(&mut self, bit: usize, contents: String) -> crate::error::Result<()>
    {
        if self.reserved_ranges.is_empty()
        {
            self.reserve(&[bit], None)?;
        }

        let col = self.matrix.last_mut().unwrap();
        col[bit] = Some(contents);
        self.in_use[bit] = true;
        Ok(())
    }

    /// Add a measurement
    ///
    /// Add a measurement of quantum bit `qbit` to classical bit `cbit` in basis
    /// `basis` to the export. If `basis` is `None`, no basis string is drawn in
    /// the measurement.
    pub fn set_measurement(&mut self, qbit: usize, cbit: usize, basis: Option<&str>)
        -> crate::error::Result<()>
    {
        let cbit_idx = self.nr_qbits + cbit;
        self.start_range_op(&[qbit], Some(&[cbit]))?;
        let meter = if let Some(b) = basis
            {
                format!(r"\meterB{{{}}}", b)
            }
            else
            {
                String::from(r"\meter")
            };
        self.set_field(qbit, meter)?;
        self.set_field(cbit_idx, format!(r"\cw \cwx[{}]", qbit as isize - cbit_idx as isize))?;
        self.end_range_op();

        Ok(())
    }

    /// Add a reset
    ///
    /// Add the reset of quantum bit `qbit` to the export.
    pub fn set_reset(&mut self, qbit: usize) -> crate::error::Result<()>
    {
        self.set_field(qbit, String::from(r"\push{~\ket{0}~} \ar @{|-{}} [0,-1]"))
    }

    /// Add classical control
    ///
    /// Add the control of an operation on quantum bits `qbits` by classical
    /// bits control to the export state. This function only adds the control
    /// part, the actual quantum operation should be drawn elsewhere. The bits
    /// in `control` make up a register, whose value should match `target`.
    /// The first bit in `control` corresponds to the least significant bit of
    /// `target`, the last bit in `control` to the most significant bit.
    pub fn set_condition(&mut self, control: &[usize], target: u64, qbits: &[usize])
        -> crate::error::Result<()>
    {
        if let Some(&bit) = qbits.iter().find(|&&b| b >= self.nr_qbits)
        {
            return Err(crate::error::Error::InvalidQBit(bit));
        }
        if let Some(&bit) = control.iter().find(|&&b| b >= self.nr_cbits)
        {
            return Err(crate::error::Error::InvalidCBit(bit));
        }

        if qbits.is_empty()
        {
            return Ok(());
        }

        let mut pbit = *qbits.iter().max().unwrap();
        let mut bp: Vec<(usize, usize)> = control.iter().enumerate()
            .map(|(pos, &idx)| (self.nr_qbits + idx, pos))
            .collect();
        bp.sort();
        for (bit, pos) in bp
        {
            let ctrl = if (target & (1 << pos)) == 0 { r"\cctrlo" } else { r"\cctrl" };
            self.set_field(bit, format!("{}{{{}}}", ctrl, pbit as isize - bit as isize))?;
            pbit = bit;
        }

        Ok(())
    }

    /// Add a simple block gate
    ///
    /// Add a gate with description `desc` operating on qubits `qbits` as a
    /// simple box containing the description. If the gate opertes on mutiple
    /// disjoint ranges of qbits, it is drawn using mutiple boxes connected by
    /// wires.
    pub fn add_block_gate(&mut self, qbits: &[usize], desc: &str) -> crate::error::Result<()>
    {
        let ranges = crate::support::get_ranges(qbits);
        if !ranges.is_empty()
        {
            self.start_range_op(qbits, None)?;

            let (first, last) = ranges[0];
            if last == first
            {
                self.set_field(first, format!(r"\gate{{{}}}", desc))?;
            }
            else
            {
                self.set_field(first,
                    format!(r"\multigate{{{}}}{{{}}}", last-first, desc))?;
                for bit in first+1..last+1
                {
                    self.set_field(bit, format!(r"\ghost{{{}}}", desc))?;
                }
            }

            let mut prev_last = last;
            for &(first, last) in ranges[1..].iter()
            {
                if last == first
                {
                    self.set_field(first,
                        format!(r"\gate{{{}}} \qwx[{}]", desc, prev_last as isize - first as isize))?;
                }
                else
                {
                    self.set_field(first,
                        format!(r"\multigate{{{}}}{{{}}} \qwx[{}]",
                            last - first, desc, prev_last as isize - first as isize))?;
                    for bit in first+1..last+1
                    {
                        self.set_field(bit, format!(r"\ghost{{{}}}", desc))?;
                    }
                }

                prev_last = last;
            }

            self.end_range_op();
        }

        Ok(())
    }

    /// Open a loop
    ///
    /// Open a loop of `count` ieterations at the current row in the export
    /// state. This loop should later be closed by a call to `end_loop()`, at
    /// which point the loop will be added to the export state.
    pub fn start_loop(&mut self, count: usize)
    {
        self.reserve_all();
        self.open_loops.push((self.matrix.len() - 1, count));
    }

    /// Close a loop
    ///
    /// Close the loop opened last by a call to `start_loop()`.
    pub fn end_loop(&mut self) -> crate::error::Result<()>
    {
        if let Some((start, count)) = self.open_loops.pop()
        {
            let end = self.matrix.len() - 1;
            self.loops.push((start, end, count));
            self.reserve_all();
            Ok(())
        }
        else
        {
            Err(crate::error::Error::from(crate::error::ExportError::CantCloseLoop))
        }
    }

    /// Add dots
    ///
    /// This function adds the string in `label` in the middle of the range of
    /// qbits starting at `bit` and going `count` bits down. This is usually
    /// used to add the dots used to indicate a repeated subcircuit in loops.
    pub fn add_cds(&mut self, bit: usize, count: usize, label: &str) -> crate::error::Result<()>
    {
        self.reserve_all();
        self.set_field(bit, format!(r"\cds{{{}}}{{{}}}", count, label))?;
        self.reserve_all();
        Ok(())
    }

    /// Add a barrier
    ///
    /// Add a barrier for the quantum bits in `qbits`. Note that the placement
    /// of barriers may sometimes be off because the spacing between elements
    /// is not constant. It may therefore need some manual adjustment.
    pub fn set_barrier(&mut self, qbits: &[usize]) -> crate::error::Result<()>
    {
        if let Some(&bit) = qbits.iter().find(|&&b| b >= self.nr_qbits)
        {
            return Err(crate::error::Error::InvalidQBit(bit));
        }

        let ranges = crate::support::get_ranges(qbits);

        self.add_column();
        for (first, last) in ranges
        {
            self.set_field(first, format!(r"\qw \barrier{{{}}}", last - first))?;
        }

        Ok(())
    }

    /// Export to LaTeX
    ///
    /// This code exports the matrix that was built up in this state to LaTeX
    /// code. It uses the qcircuit package to do so.
    pub fn code(&self) -> String
    {
        let mut res = String::from("\\Qcircuit @C=1em @R=.7em {\n");

        if !self.loops.is_empty()
        {
            let mut prev_idx = 0;
            res += r"    & ";
            for (start, end, count) in self.loops.iter()
            {
                res += r"& ".repeat(start - prev_idx).as_str();
                res += format!("\\mbox{{}} \\POS\"{},{}\".\"{},{}\".\"{},{}\".\"{},{}\"!C*+<.7em>\\frm{{^\\}}}},+U*++!D{{{}\\times}}",
                    2, start+2, 2, start+2, /*self.total_nr_bits()+*/2, end+2,
                    /*self.total_nr_bits()+*/2, end+2, count).as_str();
                prev_idx = *start;
            }
            res += "\\\\\n";

            res += r"    ";
            res += r"& ".repeat(self.matrix.len()).as_str();
            res += "\\\\\n";
        }

        let last_col_used = self.in_use.contains(&true);
        for i in 0..self.total_nr_bits()
        {
            if self.add_init
            {
                if i < self.nr_qbits
                {
                    res += r"    \lstick{\ket{0}}";
                }
                else
                {
                    res += r"    \lstick{0}";
                }
            }
            else
            {
                res += r"    ";
            }
            for row in self.matrix.iter()
            {
                res += " & ";
                if let Some(ref s) = row[i]
                {
                    res += s.as_str();
                }
                else if i < self.nr_qbits
                {
                    res += r"\qw";
                }
                else
                {
                    res += r"\cw";
                }
            }

            if last_col_used
            {
                res += r" & ";
                res += if i < self.nr_qbits { r"\qw" } else { r"\cw" };
            }
            res += " \\\\\n";
        }
        res += "}\n";

        res
    }

    /// Set whether gates are controlled
    ///
    /// This sets the option to draw gates in their normal layout
    /// (`controlled = false`) or in their controlled layout (when
    /// `controlled = true`). This can make a difference for e.g. the X gate,
    /// which is drawn as a boxed X character normally, but as an exclusive
    /// or symbol (⊕) when used in a `CX` gate.
    pub fn set_controlled(&mut self, controlled: bool) -> bool
    {
        let res = self.controlled;
        self.controlled = controlled;
        res
    }

    /// Return whether gates should currently be drawn normally, or in their
    /// controlled form.
    pub fn is_controlled(&self) -> bool
    {
        self.controlled
    }

    /// Set whether to expand composite gates.
    ///
    /// Set whether composite gates should be drawn as individual components.
    /// If `expand` is `true`, composite gates are drawn by drawing their
    /// components. If `expand` is false, composite gates are drawn as a single
    /// block gate.
    pub fn set_expand_composite(&mut self, expand: bool)
    {
        self.expand_composite = expand;
    }

    /// Whether to expand composite gates.
    ///
    /// Return whether composite gates should be drawn as individual components
    /// (in which case `true` is returned), or as a single, possibly multi-bit,
    /// operation (when the result is `false`).
    pub fn expand_composite(&self) -> bool
    {
        self.expand_composite
    }

    /// Set whether to add initialization strings
    ///
    /// When `add_init` is `true`, initialization strings are added to the
    /// bits, when `false` they are omitted.
    pub fn set_add_init(&mut self, add_init: bool)
    {
        self.add_init = add_init;
    }
}

/// Trait for gates that can be drawn in LaTeX
pub trait Latex: crate::gates::Gate
{
    /// Add this gate to the export state.
    ///
    /// Add the execution of this gate on the bits in `bits`, to the export
    /// state `state`. The default implementation returns a NotImplemented error.
    fn latex(&self, bits: &[usize], state: &mut LatexExportState)
        -> crate::error::Result<()>
    {
        self.check_nr_bits(bits.len())?;
        state.add_block_gate(bits, self.description())
    }
}

#[cfg(test)]
mod tests
{
    use super::{LatexExportState, Latex};

    struct NoLatexGate;

    impl crate::gates::Gate for NoLatexGate
    {
        fn cost(&self) -> f64 { 0.0 }
        fn description(&self) -> &str { "NLG" }
        fn nr_affected_bits(&self) -> usize { 3 }
        fn matrix(&self) -> crate::cmatrix::CMatrix { unimplemented!() }
    }
    impl Latex for NoLatexGate {}

    #[test]
    fn test_new()
    {
        let nr_qbits = 5;
        let nr_cbits = 2;
        let state = LatexExportState::new(nr_qbits, nr_cbits);
        assert_eq!(state.nr_qbits, nr_qbits);
        assert_eq!(state.nr_cbits, nr_cbits);
        assert_eq!(state.add_init, true);
        assert_eq!(state.expand_composite, true);
        assert_eq!(state.matrix, Vec::<Vec<Option<String>>>::new());
        assert_eq!(state.in_use, vec![true; nr_qbits+nr_cbits]);
        assert_eq!(state.controlled, false);
        assert_eq!(state.loops, vec![]);
        assert_eq!(state.open_loops, vec![]);
    }

    #[test]
    fn test_total_nr_bits()
    {
        let state = LatexExportState::new(5, 2);
        assert_eq!(state.total_nr_bits(), 7);

        let state = LatexExportState::new(3, 0);
        assert_eq!(state.total_nr_bits(), 3);

        let state = LatexExportState::new(2, 8);
        assert_eq!(state.total_nr_bits(), 10);
    }

    #[test]
    fn test_add_column()
    {
        let mut state = LatexExportState::new(3, 1);
        state.add_column();
        assert_eq!(state.matrix, vec![vec![None, None, None, None]]);

        state.matrix[0][1] = Some(String::from("x"));
        state.add_column();
        assert_eq!(state.matrix, vec![
            vec![None, Some(String::from("x")), None, None],
            vec![None, None, None, None]
        ]);
    }

    #[test]
    fn test_reserve()
    {
        let mut state = LatexExportState::new(2, 2);
        assert_eq!(state.reserve(&[0], None), Ok(()));
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]]);

        state.in_use[0] = true;
        assert_eq!(state.reserve(&[1], None), Ok(()));
        assert_eq!(state.in_use, vec![true, false, false, false]);
        assert_eq!(state.matrix, vec![vec![None, None, None, None]]);

        assert_eq!(state.reserve(&[0], None), Ok(()));
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 2]);

        state.in_use[3] = true;
        assert_eq!(state.reserve(&[0, 1], None), Ok(()));
        assert_eq!(state.in_use, vec![false, false, false, true]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 2]);

        assert_eq!(state.reserve(&[0, 1], Some(&[0])), Ok(()));
        assert_eq!(state.in_use, vec![false, false, false, true]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 2]);

        assert_eq!(state.reserve(&[0, 1], Some(&[1])), Ok(()));
        assert_eq!(state.in_use, vec![false, false, false, false]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 3]);
    }

    #[test]
    fn test_reserve_all()
    {
        let mut state = LatexExportState::new(2, 2);
        state.reserve_all();
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]]);

        state.reserve_all();
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]]);

        state.in_use[0] = true;
        state.reserve_all();
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 2]);
    }

    #[test]
    fn test_range_op()
    {
        let mut state = LatexExportState::new(2, 2);
        assert_eq!(state.start_range_op(&[0], None), Ok(()));
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]]);
        state.end_range_op();
        assert_eq!(state.in_use, vec![true, false, false, false]);
        assert_eq!(state.matrix, vec![vec![None; 4]]);

        assert_eq!(state.start_range_op(&[0, 1], None), Ok(()));
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 2]);
        state.end_range_op();
        assert_eq!(state.in_use, vec![true, true, false, false]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 2]);

        assert_eq!(state.start_range_op(&[0], None), Ok(()));
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 3]);
        state.end_range_op();
        assert_eq!(state.in_use, vec![true, false, false, false]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 3]);

        assert_eq!(state.start_range_op(&[1], None), Ok(()));
        assert_eq!(state.in_use, vec![true, false, false, false]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 3]);
        state.end_range_op();
        assert_eq!(state.in_use, vec![true, true, false, false]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 3]);

        assert_eq!(state.start_range_op(&[0], Some(&[1])), Ok(()));
        assert_eq!(state.in_use, vec![false; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 4]);
        state.end_range_op();
        assert_eq!(state.in_use, vec![true; 4]);
        assert_eq!(state.matrix, vec![vec![None; 4]; 4]);
    }

    #[test]
    fn test_set_field()
    {
        let mut state = LatexExportState::new(2, 0);
        assert_eq!(state.set_field(0, String::from("hello")), Ok(()));
        assert_eq!(state.in_use, vec![true, false]);
        assert_eq!(state.matrix, vec![
            vec![Some(String::from("hello")), None]
        ]);

        assert_eq!(state.set_field(1, String::from("world")), Ok(()));
        assert_eq!(state.in_use, vec![true, true]);
        assert_eq!(state.matrix, vec![
            vec![Some(String::from("hello")), Some(String::from("world"))]
        ]);

        assert_eq!(state.set_field(0, String::from("hi there")), Ok(()));
        assert_eq!(state.in_use, vec![true, false]);
        assert_eq!(state.matrix, vec![
            vec![Some(String::from("hello")), Some(String::from("world"))],
            vec![Some(String::from("hi there")), None]
        ]);

        state.add_column();
        assert_eq!(state.set_field(1, String::from("planet Mars")), Ok(()));
        assert_eq!(state.in_use, vec![false, true]);
        assert_eq!(state.matrix, vec![
            vec![Some(String::from("hello")), Some(String::from("world"))],
            vec![Some(String::from("hi there")), None],
            vec![None, Some(String::from("planet Mars"))]
        ]);
    }

    #[test]
    fn test_set_measurement()
    {
        let mut state = LatexExportState::new(2, 2);
        assert_eq!(state.set_measurement(0, 1, None), Ok(()));
        assert_eq!(state.set_measurement(1, 0, Some("X")), Ok(()));
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \meter & \qw & \qw \\
    \lstick{\ket{0}} & \qw & \meterB{X} & \qw \\
    \lstick{0} & \cw & \cw \cwx[-1] & \cw \\
    \lstick{0} & \cw \cwx[-3] & \cw & \cw \\
}
"#);
    }

    #[test]
    fn test_set_reset()
    {
        let mut state = LatexExportState::new(2, 0);
        assert_eq!(state.set_reset(0), Ok(()));
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \push{~\ket{0}~} \ar @{|-{}} [0,-1] & \qw \\
    \lstick{\ket{0}} & \qw & \qw \\
}
"#);
    }

    #[test]
    fn test_set_condition()
    {
        let mut state = LatexExportState::new(2, 2);
        assert_eq!(state.start_range_op(&[], None), Ok(()));
        assert_eq!(state.set_condition(&[0, 1], 2, &[]), Ok(()));
        state.end_range_op();

        assert_eq!(state.start_range_op(&[0], Some(&[0, 1])), Ok(()));
        assert_eq!(state.set_field(0, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_condition(&[0, 1], 2, &[0]), Ok(()));
        state.end_range_op();

        assert_eq!(state.start_range_op(&[1], Some(&[0, 1])), Ok(()));
        assert_eq!(state.set_field(1, String::from(r"\gate{H}")), Ok(()));
        assert_eq!(state.set_condition(&[0, 1], 1, &[1]), Ok(()));
        state.end_range_op();

        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \gate{X} & \qw & \qw \\
    \lstick{\ket{0}} & \qw & \gate{H} & \qw \\
    \lstick{0} & \cctrlo{-2} & \cctrl{-1} & \cw \\
    \lstick{0} & \cctrl{-1} & \cctrlo{-1} & \cw \\
}
"#);
    }

    #[test]
    fn test_add_block_gate()
    {
        let mut state = LatexExportState::new(1, 0);
        assert_eq!(state.add_block_gate(&[0], "G"), Ok(()));
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \gate{G} & \qw \\
}
"#);

        let mut state = LatexExportState::new(3, 0);
        assert_eq!(state.add_block_gate(&[1, 0, 2], "G"), Ok(()));
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \multigate{2}{G} & \qw \\
    \lstick{\ket{0}} & \ghost{G} & \qw \\
    \lstick{\ket{0}} & \ghost{G} & \qw \\
}
"#);

        let mut state = LatexExportState::new(5, 0);
        assert_eq!(state.add_block_gate(&[1, 0, 3], "G"), Ok(()));
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \multigate{1}{G} & \qw \\
    \lstick{\ket{0}} & \ghost{G} & \qw \\
    \lstick{\ket{0}} & \qw & \qw \\
    \lstick{\ket{0}} & \gate{G} \qwx[-2] & \qw \\
    \lstick{\ket{0}} & \qw & \qw \\
}
"#);
    }

    #[test]
    fn test_loop()
    {
        let mut state = LatexExportState::new(2, 0);
        state.start_loop(23);
        assert_eq!(state.reserve(&[0, 1], None), Ok(()));
        assert_eq!(state.set_field(0, String::from(r"\gate{H}")), Ok(()));
        assert_eq!(state.set_field(1, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.add_cds(0, 1, r"\leftrightarrow"), Ok(()));
        assert_eq!(state.reserve(&[0, 1], None), Ok(()));
        assert_eq!(state.set_field(0, String::from(r"\gate{H}")), Ok(()));
        assert_eq!(state.set_field(1, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.end_loop(), Ok(()));

        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    & \mbox{} \POS"2,2"."2,2"."2,4"."2,4"!C*+<.7em>\frm{^\}},+U*++!D{23\times}\\
    & & & & \\
    \lstick{\ket{0}} & \gate{H} & \cds{1}{\leftrightarrow} & \gate{H} & \qw \\
    \lstick{\ket{0}} & \gate{X} & \qw & \gate{X} & \qw \\
}
"#);
    }

    #[test]
    fn test_loop_close_error()
    {
        let mut state = LatexExportState::new(2, 0);
        assert_eq!(state.end_loop(),
            Err(crate::error::Error::ExportError(crate::error::ExportError::CantCloseLoop))
        );
    }

    #[test]
    fn test_set_barrier()
    {
        let mut state = LatexExportState::new(3, 0);
        assert_eq!(state.set_field(0, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_field(1, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_field(2, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_barrier(&[0]), Ok(()));

        assert_eq!(state.set_field(0, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_field(1, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_field(2, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_barrier(&[0, 2]), Ok(()));

        assert_eq!(state.set_field(0, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_field(1, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_field(2, String::from(r"\gate{X}")), Ok(()));
        assert_eq!(state.set_barrier(&[0, 1, 2]), Ok(()));

        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \gate{X} & \qw \barrier{0} & \gate{X} & \qw \barrier{0} & \gate{X} & \qw \barrier{2} & \qw \\
    \lstick{\ket{0}} & \gate{X} & \qw & \gate{X} & \qw & \gate{X} & \qw & \qw \\
    \lstick{\ket{0}} & \gate{X} & \qw & \gate{X} & \qw \barrier{0} & \gate{X} & \qw & \qw \\
}
"#);
    }

    #[test]
    fn test_no_init()
    {
        let mut state = LatexExportState::new(1, 1);
        assert_eq!(state.set_measurement(0, 0, None), Ok(()));

        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \meter & \qw \\
    \lstick{0} & \cw \cwx[-1] & \cw \\
}
"#);

        state.set_add_init(false);
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
     & \meter & \qw \\
     & \cw \cwx[-1] & \cw \\
}
"#);
    }

    #[test]
    fn test_default_trait_impl()
    {
        let gate = NoLatexGate {};
        let mut state = LatexExportState::new(5, 0);
        assert_eq!(gate.latex(&[1, 2, 4], &mut state), Ok(()));
        assert_eq!(gate.latex(&[0, 2, 3], &mut state), Ok(()));
        assert_eq!(state.code(),
r#"\Qcircuit @C=1em @R=.7em {
    \lstick{\ket{0}} & \qw & \gate{NLG} & \qw \\
    \lstick{\ket{0}} & \multigate{1}{NLG} & \qw & \qw \\
    \lstick{\ket{0}} & \ghost{NLG} & \multigate{1}{NLG} \qwx[-2] & \qw \\
    \lstick{\ket{0}} & \qw & \ghost{NLG} & \qw \\
    \lstick{\ket{0}} & \gate{NLG} \qwx[-2] & \qw & \qw \\
}
"#);
    }
}