|
10 | 10 | // copyright notice, and modified files need to carry a notice indicating |
11 | 11 | // that they have been altered from the originals. |
12 | 12 |
|
| 13 | +use ndarray::{Array2, array}; |
13 | 14 | use num_complex::Complex64; |
14 | 15 | use std::f64::consts::FRAC_1_SQRT_2; |
15 | 16 |
|
@@ -514,3 +515,57 @@ pub fn xx_plus_yy_gate(theta: f64, beta: f64) -> GateArray2Q { |
514 | 515 | [C_ZERO, C_ZERO, C_ZERO, C_ONE], |
515 | 516 | ] |
516 | 517 | } |
| 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