microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/paulimer/src/clifford/special_clifford.rs
99lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use super::{CliffordUnitary, ControlledPauli, Hadamard, PauliExponent, Swap}; |
| 5 | use crate::pauli::{commutes_with, generic::PhaseExponent, Pauli, PauliBits, PauliUnitary}; |
| 6 | use std::ops::Mul; |
| 7 | |
| 8 | impl<Bits: PauliBits, Phase: PhaseExponent> ControlledPauli<Bits, Phase> { |
| 9 | /// # Panics |
| 10 | /// |
| 11 | /// Will panic |
| 12 | pub fn new( |
| 13 | control: PauliUnitary<Bits, Phase>, |
| 14 | target: PauliUnitary<Bits, Phase>, |
| 15 | ) -> ControlledPauli<Bits, Phase> { |
| 16 | assert!(commutes_with(&control, &target)); |
| 17 | assert!(control.is_order_two()); |
| 18 | assert!(target.is_order_two()); |
| 19 | ControlledPauli(control, target) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | impl<Bits: PauliBits, Phase: PhaseExponent> PauliExponent<Bits, Phase> { |
| 24 | /// # Panics |
| 25 | /// |
| 26 | /// Will panic |
| 27 | pub fn new(pauli: PauliUnitary<Bits, Phase>) -> PauliExponent<Bits, Phase> { |
| 28 | assert!(pauli.is_order_two()); |
| 29 | PauliExponent(pauli) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | macro_rules! delegate_left_multiplication_template_variants { |
| 34 | ($left:ident) => { |
| 35 | impl<Bits: PauliBits, _Phase: PhaseExponent> Mul<CliffordUnitary> for &$left<Bits, _Phase> { |
| 36 | type Output = CliffordUnitary; |
| 37 | |
| 38 | fn mul(self, mut clifford: CliffordUnitary) -> Self::Output { |
| 39 | self * &mut clifford; |
| 40 | clifford |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl<Bits: PauliBits, _Phase: PhaseExponent> Mul<&mut CliffordUnitary> |
| 45 | for $left<Bits, _Phase> |
| 46 | { |
| 47 | type Output = (); |
| 48 | |
| 49 | fn mul(self, clifford: &mut CliffordUnitary) -> Self::Output { |
| 50 | &self * clifford; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl<Bits: PauliBits, _Phase: PhaseExponent> Mul<CliffordUnitary> for $left<Bits, _Phase> { |
| 55 | type Output = CliffordUnitary; |
| 56 | |
| 57 | fn mul(self, mut clifford: CliffordUnitary) -> Self::Output { |
| 58 | &self * &mut clifford; |
| 59 | clifford |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | macro_rules! delegate_left_multiplication_variants { |
| 66 | ($left:ty) => { |
| 67 | impl Mul<CliffordUnitary> for &$left { |
| 68 | type Output = CliffordUnitary; |
| 69 | |
| 70 | fn mul(self, mut clifford: CliffordUnitary) -> Self::Output { |
| 71 | self * &mut clifford; |
| 72 | clifford |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | impl Mul<&mut CliffordUnitary> for $left { |
| 77 | type Output = (); |
| 78 | |
| 79 | fn mul(self, clifford: &mut CliffordUnitary) -> Self::Output { |
| 80 | &self * clifford; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | impl Mul<CliffordUnitary> for $left { |
| 85 | type Output = CliffordUnitary; |
| 86 | |
| 87 | fn mul(self, mut clifford: CliffordUnitary) -> Self::Output { |
| 88 | &self * &mut clifford; |
| 89 | clifford |
| 90 | } |
| 91 | } |
| 92 | }; |
| 93 | } |
| 94 | |
| 95 | delegate_left_multiplication_template_variants!(PauliUnitary); |
| 96 | delegate_left_multiplication_template_variants!(ControlledPauli); |
| 97 | delegate_left_multiplication_template_variants!(PauliExponent); |
| 98 | delegate_left_multiplication_variants!(Swap); |
| 99 | delegate_left_multiplication_variants!(Hadamard); |
| 100 | |