Skip to content

Commit 27d8136

Browse files
committed
Add dense matrix support for PauliProductRotation
1 parent 50c18fa commit 27d8136

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

crates/circuit/src/gate_matrix.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// copyright notice, and modified files need to carry a notice indicating
1111
// that they have been altered from the originals.
1212

13+
use ndarray::{Array2, array};
1314
use num_complex::Complex64;
1415
use std::f64::consts::FRAC_1_SQRT_2;
1516

@@ -514,3 +515,57 @@ pub fn xx_plus_yy_gate(theta: f64, beta: f64) -> GateArray2Q {
514515
[C_ZERO, C_ZERO, C_ZERO, C_ONE],
515516
]
516517
}
518+
519+
pub fn pauli_zx_to_matrix(z: &[bool], x: &[bool]) -> Array2<Complex64> {
520+
debug_assert_eq!(z.len(), x.len());
521+
522+
let i = array![
523+
[Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
524+
[Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)],
525+
];
526+
let x_mat = array![
527+
[Complex64::new(0.0, 0.0), Complex64::new(1.0, 0.0)],
528+
[Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
529+
];
530+
let z_mat = array![
531+
[Complex64::new(1.0, 0.0), Complex64::new(0.0, 0.0)],
532+
[Complex64::new(0.0, 0.0), Complex64::new(-1.0, 0.0)],
533+
];
534+
let y_mat = array![
535+
[Complex64::new(0.0, 0.0), Complex64::new(0.0, -1.0)],
536+
[Complex64::new(0.0, 1.0), Complex64::new(0.0, 0.0)],
537+
];
538+
539+
fn kron(a: &Array2<Complex64>, b: &Array2<Complex64>) -> Array2<Complex64> {
540+
let (ar, ac) = a.dim();
541+
let (br, bc) = b.dim();
542+
let mut out = Array2::<Complex64>::zeros((ar * br, ac * bc));
543+
544+
for i in 0..ar {
545+
for j in 0..ac {
546+
let coeff = a[(i, j)];
547+
for k in 0..br {
548+
for l in 0..bc {
549+
out[(i * br + k, j * bc + l)] = coeff * b[(k, l)];
550+
}
551+
}
552+
}
553+
}
554+
555+
out
556+
}
557+
558+
let mut result = array![[Complex64::new(1.0, 0.0)]];
559+
560+
for (&z_bit, &x_bit) in z.iter().zip(x.iter()) {
561+
let factor = match (z_bit, x_bit) {
562+
(false, false) => &i,
563+
(false, true) => &x_mat,
564+
(true, false) => &z_mat,
565+
(true, true) => &y_mat,
566+
};
567+
result = kron(&result, factor);
568+
}
569+
570+
result
571+
}

0 commit comments

Comments
 (0)