Skip to content

Commit 0d9aa3d

Browse files
committed
Test: Further fixes to tests
- Removed most print statements in favor of python comparisons.
1 parent 21b283e commit 0d9aa3d

1 file changed

Lines changed: 36 additions & 5 deletions

File tree

‎crates/circuit/src/operations.rs‎

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,7 @@ mod test_custom_operations {
22262226
use crate::packed_instruction::PackedOperation;
22272227
use crate::{Clbit, Qubit};
22282228
use ndarray::aview2;
2229+
use numpy::{Complex64, PyArray2, PyArrayMethods};
22292230
use pyo3::prelude::*;
22302231
use smallvec::smallvec;
22312232
use std::f64::consts::PI;
@@ -2863,10 +2864,41 @@ mod test_custom_operations {
28632864
for (idx, inst) in circuit.data().iter().enumerate() {
28642865
let unpacked_operation = circuit.unpack_py_op(py, inst)?.into_bound(py);
28652866

2866-
println!("{}", unpacked_operation.repr()?);
2867-
println!("{}", unpacked_operation.call_method0("to_matrix")?.repr()?);
2868-
println!("{}", unpacked_operation.getattr("params")?.repr()?);
2869-
println!("{}", unpacked_operation.getattr("definition")?.repr()?);
2867+
// Compare repr
2868+
assert_eq!(
2869+
format!(
2870+
"Instruction(name='{}', num_qubits={}, num_clbits={}, params={:?})",
2871+
gates[idx].name(),
2872+
gates[idx].num_qubits(),
2873+
gates[idx].num_clbits(),
2874+
inst.params_view()
2875+
.iter()
2876+
.filter_map(|params| params.try_float())
2877+
.collect::<Vec<_>>(),
2878+
),
2879+
unpacked_operation.repr()?.to_string()
2880+
);
2881+
2882+
// Compare python matrix
2883+
let orig_matrix = gates[idx]
2884+
.matrix(inst.params_view())
2885+
.expect("The provided parameters should generate the matrix");
2886+
let retrieved_array = unpacked_operation
2887+
.call_method0("to_matrix")?
2888+
.cast_into::<PyArray2<Complex64>>()?;
2889+
let retrieved_matrix = retrieved_array.to_owned_array();
2890+
assert!(approx::abs_diff_eq!(
2891+
orig_matrix,
2892+
retrieved_matrix,
2893+
epsilon = 1e-12
2894+
));
2895+
2896+
// Compare params
2897+
assert!(
2898+
unpacked_operation
2899+
.getattr("params")?
2900+
.eq(inst.params_view().into_pyobject(py)?)?
2901+
);
28702902

28712903
// Try roundtrip
28722904
let roundtrip = unpacked_operation.extract::<OperationFromPython<CircuitData>>()?;
@@ -2878,7 +2910,6 @@ mod test_custom_operations {
28782910
);
28792911
};
28802912

2881-
println!("{:?}", roundtrip.operation.view());
28822913
assert_eq!(gates[idx], roundtrip_native);
28832914
assert_eq!(gates[idx].is_unitary(), roundtrip_native.is_unitary());
28842915
assert_eq!(

0 commit comments

Comments
 (0)