From 9ac49a38d48940fdd190487725e783ceb3e3fb6b Mon Sep 17 00:00:00 2001 From: Nicolas Vogt <45093608+nfwvogt@users.noreply.github.com> Date: Thu, 18 Nov 2021 19:14:47 +0100 Subject: [PATCH] Fix_multi_qubit_zz (#133) * update dependencies * updated dependencies * fixing bug in MultiQubitZZ gate * Update multi_qubit_gate_operations.rs Co-authored-by: kbarkhqs <65908581+kbarkhqs@users.noreply.github.com> --- roqoqo/src/operations/multi_qubit_gate_operations.rs | 12 +++++++++++- .../operations/multi_qubit_gate_operations.rs | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/roqoqo/src/operations/multi_qubit_gate_operations.rs b/roqoqo/src/operations/multi_qubit_gate_operations.rs index 75dc939a..daca65dc 100644 --- a/roqoqo/src/operations/multi_qubit_gate_operations.rs +++ b/roqoqo/src/operations/multi_qubit_gate_operations.rs @@ -10,6 +10,8 @@ // express or implied. See the License for the specific language governing permissions and // limitations under the License. +use std::panic; + use crate::operations; use crate::prelude::*; use crate::Circuit; @@ -127,7 +129,15 @@ impl OperateGate for MultiQubitZZ { let cos: Complex64 = Complex64::new((self.theta.float()? / 2.0).cos(), 0.0); let sin: Complex64 = Complex64::new(0.0, -(self.theta.float()? / 2.0).sin()); for i in 0..dim { - array[(i, i)] = cos + sin; + // Fix the signs of the imaginary part due to the ZZZ..ZZ product + let prefactor: f64 = (0..self.qubits.len()) + .map(|q| match i.div_euclid(2usize.pow(q as u32)) % 2 { + 0 => 1.0, + 1 => -1.0, + _ => panic!("Internal division error MuliQubitZZ"), + }) + .product(); + array[(i, i)] = cos + prefactor * sin; } Ok(array) } diff --git a/roqoqo/tests/integration/operations/multi_qubit_gate_operations.rs b/roqoqo/tests/integration/operations/multi_qubit_gate_operations.rs index 29a9c01a..fe444cd5 100644 --- a/roqoqo/tests/integration/operations/multi_qubit_gate_operations.rs +++ b/roqoqo/tests/integration/operations/multi_qubit_gate_operations.rs @@ -337,14 +337,14 @@ fn test_matrix_output_multi_qubit_zz(qubits: Vec) { ], [ Complex64::new(0.0, 0.0), - Complex64::new(f, (-1.0) * f), + Complex64::new(f, f), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0) ], [ Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), - Complex64::new(f, (-1.0) * f), + Complex64::new(f, f), Complex64::new(0.0, 0.0) ], [ @@ -376,7 +376,7 @@ fn test_matrix_output_three_multi_qubit_zz(qubits: Vec) { ], [ Complex64::new(0.0, 0.0), - Complex64::new(f, (-1.0) * f), + Complex64::new(f, f), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), @@ -387,7 +387,7 @@ fn test_matrix_output_three_multi_qubit_zz(qubits: Vec) { [ Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), - Complex64::new(f, (-1.0) * f), + Complex64::new(f, f), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), @@ -409,7 +409,7 @@ fn test_matrix_output_three_multi_qubit_zz(qubits: Vec) { Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), - Complex64::new(f, (-1.0) * f), + Complex64::new(f, f), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0) @@ -442,7 +442,7 @@ fn test_matrix_output_three_multi_qubit_zz(qubits: Vec) { Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), Complex64::new(0.0, 0.0), - Complex64::new(f, (-1.0) * f) + Complex64::new(f, f) ], ]; let unit = gate.unitary_matrix().unwrap();