qudit-circuit 0.3.2

Accelerated and Extensible Quantum Library
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
use std::collections::HashSet;

use crate::{compact::CompactIntegerVector, point::Wire};

// use tinyvec::TinyVec;

// #[derive(Hash, PartialEq, Eq, Clone, Debug)]
// pub struct Wirelist(Vec<Wire>);

// impl WireList {

// }

/// A CircuitLocation describes where an instruction is spatial executed.
///
/// In other words, it describes a register of circuit qudits and classical
/// dits. This is commonly used to indicate "where" an instruction acts.
/// The location object respects the order of the qudits and dits, i.e.,
/// the desired permutation of the gate by the order of the qudits.
///
/// Consider a four-qubit circuit, the location with qudits = [0, 2], and
/// dits = [] represents the two-qudit purely-quantum register of the first
/// and third qudits in the circuit. This location is not equivalent to the
/// location with qudits = [2, 0], and dits = [], as this describes a
/// permutation of the first example.
#[derive(Hash, PartialEq, Eq, Clone, Debug)]
pub struct CircuitLocation {
    /// The described qudits in the circuit.
    qudits: CompactIntegerVector,

    /// The described dits in the circuit.
    dits: CompactIntegerVector,
}

impl CircuitLocation {
    /// Returns a purely-quantum CircuitLocation object from the given vector.
    ///
    /// A purely-quantum location is one that does not contain any classical dits.
    ///
    /// # Arguments
    ///
    /// * `location` - A vector describing the qudits in a circuit.
    ///
    /// # Panics
    ///
    /// If `location` is not a valid location. This can happen if a qudit
    /// index appears twice in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure(&vec![3, 0]);
    /// ```
    pub fn pure<T: AsRef<[usize]>>(location: T) -> CircuitLocation {
        CircuitLocation::new(location, &[])
    }

    /// Returns a purely-classical CircuitLocation object from the given vector.
    ///
    /// A purely-classical location is one that does not contain any qudits.
    ///
    /// # Arguments
    ///
    /// * `location` - A vector describing the classical dits in a circuit.
    ///
    /// # Panics
    ///
    /// If `location` is not a valid location. This can happen if a dit
    /// index appears twice in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::classical(&vec![1, 2]);
    /// ```
    pub fn classical<T: AsRef<[usize]>>(location: T) -> CircuitLocation {
        CircuitLocation::new(&[], location)
    }

    // TODO: from array of CircuitDitIds

    /// Returns a CircuitLocation object from the given vectors.
    ///
    /// # Arguments
    ///
    /// * `qudits` - A vector describing the qudits in a circuit.
    ///
    /// * `dits` - A vector describing the classical dits in a circuit.
    ///
    /// # Panics
    ///
    /// If `qudits` or `dits` are not valid locations. This can happen if a
    /// qudit or clbit index appears twice in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new(&vec![3, 0], &vec![1, 2]);
    /// ```
    pub fn new<S: AsRef<[usize]>, T: AsRef<[usize]>>(qudits: S, dits: T) -> CircuitLocation {
        let qudits = qudits.as_ref();
        let dits = dits.as_ref();
        // Uniqueness check // TODO: re-evaluate 20 below; maybe surround in
        // debug
        if qudits.len() < 20 && dits.len() < 20 {
            // Performance: Locatins are typically small, so this O(N^2)
            // uniqueness check is faster than the O(N) HashSet check.
            // This speed up is because of a low constant factor and
            // the fact that the HashSet check has to allocate memory.

            for i in 0..qudits.len() {
                for j in (i + 1)..qudits.len() {
                    if qudits[i] == qudits[j] {
                        panic!("Duplicate indices in given circuit location.");
                    }
                }
            }

            for i in 0..dits.len() {
                for j in (i + 1)..dits.len() {
                    if dits[i] == dits[j] {
                        panic!("Duplicate indices in given circuit location.");
                    }
                }
            }
        } else {
            let mut uniq = HashSet::new();
            if !qudits.iter().all(|x| uniq.insert(x)) {
                panic!("Duplicate indices in given circuit location.");
            }
            uniq.clear();
            if !dits.iter().all(|x| uniq.insert(x)) {
                panic!("Duplicate indices in given circuit location.");
            }
        }

        CircuitLocation {
            qudits: CompactIntegerVector::from(qudits),
            dits: CompactIntegerVector::from(dits),
        }
    }

    /// Get the qudits in this location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new(vec![3, 0], vec![1, 2]);
    /// assert_eq!(loc.qudits(), &[3, 0]);
    /// ```
    pub fn qudits(&self) -> &CompactIntegerVector {
        &self.qudits
    }

    /// Get the classical dits in this location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new([3, 0], [1, 2]);
    /// assert_eq!(loc.dits(), &[1, 2]);
    /// ```
    pub fn dits(&self) -> &CompactIntegerVector {
        &self.dits
    }

    /// Returns a new location containing all elements in `self` or `other`
    ///
    /// # Arguments
    ///
    /// * `other` - The other location to union.
    ///
    /// # Notes
    ///
    /// * The output orders the elements from `self` first, then the ones from
    ///   `other`.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::pure([0, 2]);
    /// let loc2 = CircuitLocation::pure([2, 3]);
    /// assert_eq!(loc1.union(&loc2), CircuitLocation::pure([0, 2, 3]));
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::new([3, 0], [1, 2]);
    /// let loc2 = CircuitLocation::new([2, 3], [3, 2]);
    /// assert_eq!(loc1.union(&loc2), CircuitLocation::new([3, 0, 2], [1, 2, 3]));
    /// ```
    pub fn union(&self, other: &CircuitLocation) -> CircuitLocation {
        let mut union_qudits = self.qudits.clone();
        for qudit_index in &other.qudits {
            if !union_qudits.contains(qudit_index) {
                union_qudits.push(qudit_index);
            }
        }

        let mut union_dits = self.dits.clone();
        for clbit_index in &other.dits {
            if !union_dits.contains(clbit_index) {
                union_dits.push(clbit_index);
            }
        }

        CircuitLocation {
            qudits: union_qudits,
            dits: union_dits,
        }
    }

    /// Returns a new location containing all elements in `self` and `other`
    ///
    /// # Arguments
    ///
    /// * `other` - The other location to intersect.
    ///
    /// # Notes
    ///
    /// * The elements in output are ordered the same way they are ordered in
    ///   `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::pure([0, 2]);
    /// let loc2 = CircuitLocation::pure([2, 3]);
    /// assert_eq!(loc1.intersect(&loc2), CircuitLocation::pure([2]));
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::classical([0, 2]);
    /// let loc2 = CircuitLocation::classical([2, 0]);
    /// assert_eq!(loc1.intersect(&loc2), CircuitLocation::classical([0, 2]));
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::new([3, 0], [1, 2]);
    /// let loc2 = CircuitLocation::new([2, 3], [3, 2]);
    /// assert_eq!(loc1.intersect(&loc2), CircuitLocation::new([3], [2]));
    /// ```
    pub fn intersect(&self, other: &CircuitLocation) -> CircuitLocation {
        let mut inter_qudits = CompactIntegerVector::new();
        for qudit_index in &self.qudits {
            if other.qudits.contains(qudit_index) {
                inter_qudits.push(qudit_index);
            }
        }

        let mut inter_dits = CompactIntegerVector::new();
        for clbit_index in &self.dits {
            if other.dits.contains(clbit_index) {
                inter_dits.push(clbit_index);
            }
        }

        CircuitLocation {
            qudits: inter_qudits,
            dits: inter_dits,
        }
    }

    /// Returns a new location containing elements in `self` but not in `other`
    ///
    /// # Arguments
    ///
    /// * `other` - The other location to subtract.
    ///
    /// # Notes
    ///
    /// * The elements in output are ordered the same way they are ordered in
    ///   `self`.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::pure(vec![0, 2]);
    /// let loc2 = CircuitLocation::pure(vec![2, 3]);
    /// assert_eq!(loc1.difference(&loc2), CircuitLocation::pure(vec![0]));
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::classical(vec![0, 2]);
    /// let loc2 = CircuitLocation::classical(vec![2, 0]);
    /// assert_eq!(loc1.difference(&loc2), CircuitLocation::classical(vec![]));
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc1 = CircuitLocation::new(vec![3, 0], vec![1, 2]);
    /// let loc2 = CircuitLocation::new(vec![2, 3], vec![3, 2]);
    /// assert_eq!(loc1.difference(&loc2), CircuitLocation::new(vec![0], vec![1]));
    /// ```
    pub fn difference(&self, other: &CircuitLocation) -> CircuitLocation {
        let mut diff_qudits = CompactIntegerVector::new();
        for qudit_index in &self.qudits {
            if !other.qudits.contains(qudit_index) {
                diff_qudits.push(qudit_index);
            }
        }

        let mut diff_dits = CompactIntegerVector::new();
        for clbit_index in &self.dits {
            if !other.dits.contains(clbit_index) {
                diff_dits.push(clbit_index);
            }
        }

        CircuitLocation {
            qudits: diff_qudits,
            dits: diff_dits,
        }
    }

    /// Returns all possible pairs of qudits in this location.
    ///
    /// These are returned as sorted pairs, i.e., the first element of the
    /// pair is always less than the second element.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([0, 3, 2]);
    ///
    /// let all_pairs = loc.get_qudit_pairs();
    ///
    /// for pair in [(0, 2), (0, 3), (2, 3)] {
    ///     assert!(all_pairs.contains(&pair))
    /// }
    /// ```
    pub fn get_qudit_pairs(&self) -> Vec<(usize, usize)> {
        let num_pairs = self.qudits.len() * (self.qudits.len() - 1) / 2;
        let mut to_return = Vec::with_capacity(num_pairs);
        for qudit_index1 in &self.qudits {
            for qudit_index2 in &self.qudits {
                if qudit_index1 < qudit_index2 {
                    to_return.push((qudit_index1, qudit_index2));
                }
            }
        }
        to_return
    }

    /// Returns a sorted copy of the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([2, 0, 3]);
    /// assert_eq!(loc.to_sorted(), CircuitLocation::pure([0, 2, 3]));
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new([3, 0], [2, 1]);
    /// assert_eq!(loc.to_sorted(), CircuitLocation::new([0, 3], [1, 2]));
    /// ```
    pub fn to_sorted(&self) -> CircuitLocation {
        let mut qudits_sorted = self.qudits.clone();
        let mut dits_sorted = self.dits.clone();
        qudits_sorted.sort();
        dits_sorted.sort();
        CircuitLocation {
            qudits: qudits_sorted,
            dits: dits_sorted,
        }
    }

    /// Returns true if the location is sorted and has trivial ordering.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([0, 2, 3]);
    /// assert!(loc.is_sorted());
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new([3, 0], [2, 1]);
    /// assert!(!loc.is_sorted());
    /// ```
    pub fn is_sorted(&self) -> bool {
        self.is_qudit_sorted() && self.is_dit_sorted()
    }

    /// Returns true if the qudits are sorted and have trivial ordering.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([0, 2, 3]);
    /// assert!(loc.is_qudit_sorted());
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new([0, 3], [2, 1]);
    /// assert!(loc.is_qudit_sorted());
    /// ```
    pub fn is_qudit_sorted(&self) -> bool {
        if self.qudits.len() < 2 {
            return true;
        }

        (0..(self.qudits.len() - 1))
            .all(|i| self.qudits.get(i).unwrap() < self.qudits.get(i + 1).unwrap())
    }

    /// Returns true if the dits are sorted and have trivial ordering.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::classical([0, 2, 3]);
    /// assert!(loc.is_dit_sorted());
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new([0, 3], [2, 1]);
    /// assert!(!loc.is_dit_sorted());
    /// ```
    pub fn is_dit_sorted(&self) -> bool {
        if self.dits.len() < 2 {
            return true;
        }

        (0..(self.dits.len() - 1))
            .all(|i| self.dits.get(i).unwrap() < self.dits.get(i + 1).unwrap())
    }

    /// Returns true if `qudit_index` is in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([0, 2, 3]);
    /// assert!(loc.contains_qudit(0));
    /// assert!(!loc.contains_qudit(1));
    /// ```
    pub fn contains_qudit(&self, qudit_index: usize) -> bool {
        self.qudits.contains(qudit_index)
    }

    /// Returns true if `dit_index` is in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::classical([0, 2, 3]);
    /// assert!(loc.contains_dit(0));
    /// assert!(!loc.contains_dit(1));
    /// ```
    pub fn contains_dit(&self, dit_index: usize) -> bool {
        self.dits.contains(dit_index)
    }

    /// Returns the number of qudits and dits in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([0, 2, 3]);
    /// assert_eq!(loc.len(), 3);
    /// ```
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::new([0, 3], [2, 1]);
    /// assert_eq!(loc.len(), 4);
    /// ```
    pub fn len(&self) -> usize {
        self.get_num_qudits() + self.get_num_dits()
    }

    /// Returns the number of qudits in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::pure([0, 2, 3]);
    /// assert_eq!(loc.get_num_qudits(), 3);
    /// ```
    pub fn get_num_qudits(&self) -> usize {
        self.qudits.len()
    }

    /// Returns the number of dits in the location.
    ///
    /// # Examples
    ///
    /// ```
    /// use qudit_circuit::CircuitLocation;
    /// let loc = CircuitLocation::classical([0, 2, 3]);
    /// assert_eq!(loc.get_num_dits(), 3);
    /// ```
    pub fn get_num_dits(&self) -> usize {
        self.dits.len()
    }

    /// Returns the index of the qudit in the location.
    pub fn get_qudit_index(&self, index: usize) -> Option<usize> {
        self.qudits.iter().position(|x| x == index)
    }

    /// Returns the index of the dit in the location.
    pub fn get_dit_index(&self, index: usize) -> Option<usize> {
        self.dits.iter().position(|x| x == index)
    }

    /// Returns a new CircuitLocation object with the same qudits and dits.
    pub fn to_owned(&self) -> CircuitLocation {
        let qudits = self.qudits.to_owned();
        let dits = self.dits.to_owned();
        CircuitLocation { qudits, dits }
    }

    pub fn wires(&self) -> Vec<Wire> {
        self.qudits.iter().map(|q| Wire::quantum(q)).chain(self.dits.iter().map(|c| Wire::classical(c))).collect()
    }

    pub fn wires_iter(&self) -> impl Iterator<Item = Wire> + '_ {
        self.qudits.iter().map(|q| Wire::quantum(q)).chain(self.dits.iter().map(|c| Wire::classical(c)))
    }
}

pub trait ToLocation {
    fn to_location(self) -> CircuitLocation;
}

// impl ToLocation for CircuitLocation {
//     fn to_location(self) -> CircuitLocation {
//         self
//     }
// }

impl<'a> ToLocation for &'a CircuitLocation {
    fn to_location(self) -> CircuitLocation {
        self.clone()
    }
}

impl ToLocation for usize
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::pure([self])
    }
}

impl ToLocation for Vec<usize>
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::pure(self)
    }
}

impl<'a> ToLocation for &'a [usize]
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::pure(self)
    }
}

impl<const N: usize> ToLocation for [usize; N]
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::pure(self)
    }
}

impl<const N: usize> ToLocation for &[usize; N]
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::pure(self)
    }
}

impl ToLocation for (usize, usize)
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::new([self.0], [self.1])
    }
}

impl ToLocation for (Vec<usize>, Vec<usize>)
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::new(self.0, self.1)
    }
}

impl<'a, 'b> ToLocation for (&'a [usize], &'b [usize])
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::new(self.0, self.1)
    }
}

impl<const N: usize, const M: usize> ToLocation for ([usize; N], [usize; M])
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::new(self.0, self.1)
    }
}

impl<'a, 'b, const N: usize, const M: usize> ToLocation for (&'a [usize; N], &'b [usize; M])
{
    fn to_location(self) -> CircuitLocation {
        CircuitLocation::new(self.0, self.1)
    }
}

impl<L: ToLocation> From<L> for CircuitLocation {
    fn from(value: L) -> Self {
        value.to_location()
    } 
}