From aefc5cec42d1913ae1760259130f2a939a6fc787 Mon Sep 17 00:00:00 2001 From: Purva Thakre <66048318+purva-thakre@users.noreply.github.com> Date: Wed, 15 May 2024 09:27:56 -0500 Subject: [PATCH] Missing unit tests for benchmarking circuits (#2366) * mirror circuits * randomized benchmarking * clifford t + ruff --- mitiq/benchmarks/tests/test_mirror_circuits.py | 7 +++++++ .../tests/test_randomized_benchmarking.py | 8 ++++++++ .../test_randomized_clifford_t_circuit.py | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/mitiq/benchmarks/tests/test_mirror_circuits.py b/mitiq/benchmarks/tests/test_mirror_circuits.py index 4ec9873a2c..464cdc84e7 100644 --- a/mitiq/benchmarks/tests/test_mirror_circuits.py +++ b/mitiq/benchmarks/tests/test_mirror_circuits.py @@ -195,6 +195,13 @@ def test_two_qubit_gate_unsupported(): ) +def test_two_qubit_gate_bad_probability(): + with pytest.raises( + ValueError, match="two_qubit_gate_prob must be between 0 and 1" + ): + mirror_circuits.generate_mirror_circuit(1, 2.0, nx.complete_graph(2)) + + def test_deterministic_correct_bitstrings(): """For a fixed seed, correct bitstrings should be deterministic.""" expected_correct_bitstrings = ( diff --git a/mitiq/benchmarks/tests/test_randomized_benchmarking.py b/mitiq/benchmarks/tests/test_randomized_benchmarking.py index cf5c3c21ce..c9b04224ff 100644 --- a/mitiq/benchmarks/tests/test_randomized_benchmarking.py +++ b/mitiq/benchmarks/tests/test_randomized_benchmarking.py @@ -41,3 +41,11 @@ def test_rb_conversion(n_qubits, return_type): ) for qc in circuits: assert return_type in qc.__module__ + + +def test_bad_n_qubits(): + with pytest.raises( + ValueError, match="Only generates RB circuits on one or two " + ): + for trials in [2, 3]: + generate_rb_circuits(n_qubits=4, num_cliffords=10, trials=trials) diff --git a/mitiq/benchmarks/tests/test_randomized_clifford_t_circuit.py b/mitiq/benchmarks/tests/test_randomized_clifford_t_circuit.py index d8e4a214cb..82e631a1a8 100644 --- a/mitiq/benchmarks/tests/test_randomized_clifford_t_circuit.py +++ b/mitiq/benchmarks/tests/test_randomized_clifford_t_circuit.py @@ -51,3 +51,21 @@ def test_conversion(return_type): return_type=return_type, ) assert return_type in circuit.__module__ + + +@pytest.mark.parametrize( + "test_num_qubits, test_num_twoq_cliffords, error_msg", + [ + (0, 2, "Cannot prepare a circuit with"), + (1, 2, "Need more than 2 qubits for two-qubit Clifford gates."), + ], +) +def test_invalid_input(test_num_qubits, test_num_twoq_cliffords, error_msg): + """Ensures error raised as expected.""" + with pytest.raises(ValueError, match=error_msg): + generate_random_clifford_t_circuit( + num_qubits=test_num_qubits, + num_oneq_cliffords=2, + num_twoq_cliffords=test_num_twoq_cliffords, + num_t_gates=2, + )