From 3bbafe0c888eefe52d5657c78c154d854f123b96 Mon Sep 17 00:00:00 2001 From: "Christopher J. Wood" Date: Mon, 3 Jun 2024 12:24:41 -0400 Subject: [PATCH] Add more unit tests --- .../backends/aer_simulator/test_noise.py | 19 +++++----- .../aer_simulator/test_shot_branching.py | 18 +++++----- test/terra/reference/ref_pauli_noise.py | 36 ++++++++++++------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/test/terra/backends/aer_simulator/test_noise.py b/test/terra/backends/aer_simulator/test_noise.py index 5e4a4c579c..3a7ebab34b 100644 --- a/test/terra/backends/aer_simulator/test_noise.py +++ b/test/terra/backends/aer_simulator/test_noise.py @@ -84,13 +84,13 @@ def test_readout_noise_without_basis_gates(self, method, device): result = backend.run(circ, shots=1).result() self.assertSuccess(result) - @supported_methods(ALL_METHODS) - def test_pauli_gate_noise(self, method, device): + @supported_methods(ALL_METHODS, [noise.QuantumError, noise.PauliError]) + def test_pauli_gate_noise(self, method, device, qerror_cls): """Test simulation with Pauli gate error noise model.""" backend = self.backend(method=method, device=device) shots = 1000 circuits = ref_pauli_noise.pauli_gate_error_circuits() - noise_models = ref_pauli_noise.pauli_gate_error_noise_models() + noise_models = ref_pauli_noise.pauli_gate_error_noise_models(qerror_cls) targets = ref_pauli_noise.pauli_gate_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): @@ -108,14 +108,15 @@ def test_pauli_gate_noise(self, method, device): "matrix_product_state", "extended_stabilizer", "tensor_network", - ] + ], + [noise.QuantumError, noise.PauliError], ) - def test_pauli_reset_noise(self, method, device): + def test_pauli_reset_noise(self, method, device, qerror_cls): """Test simulation with Pauli reset error noise model.""" backend = self.backend(method=method, device=device) shots = 1000 circuits = ref_pauli_noise.pauli_reset_error_circuits() - noise_models = ref_pauli_noise.pauli_reset_error_noise_models() + noise_models = ref_pauli_noise.pauli_reset_error_noise_models(qerror_cls) targets = ref_pauli_noise.pauli_reset_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): @@ -124,13 +125,13 @@ def test_pauli_reset_noise(self, method, device): self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - @supported_methods(ALL_METHODS) - def test_pauli_measure_noise(self, method, device): + @supported_methods(ALL_METHODS, [noise.QuantumError, noise.PauliError]) + def test_pauli_measure_noise(self, method, device, qerror_cls): """Test simulation with Pauli measure error noise model.""" backend = self.backend(method=method, device=device) shots = 1000 circuits = ref_pauli_noise.pauli_measure_error_circuits() - noise_models = ref_pauli_noise.pauli_measure_error_noise_models() + noise_models = ref_pauli_noise.pauli_measure_error_noise_models(qerror_cls) targets = ref_pauli_noise.pauli_measure_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): diff --git a/test/terra/backends/aer_simulator/test_shot_branching.py b/test/terra/backends/aer_simulator/test_shot_branching.py index 32b0547763..5b330a4e53 100644 --- a/test/terra/backends/aer_simulator/test_shot_branching.py +++ b/test/terra/backends/aer_simulator/test_shot_branching.py @@ -402,13 +402,13 @@ def test_shot_branching_readout_noise(self, method, device): self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - @supported_methods(SUPPORTED_METHODS) - def test_shot_branching_pauli_gate_noise(self, method, device): + @supported_methods(SUPPORTED_METHODS, [noise.QuantumError, noise.PauliError]) + def test_shot_branching_pauli_gate_noise(self, method, device, qerror_cls): """Test simulation with Pauli gate error noise model.""" backend = self.backend(method=method, device=device) shots = 1000 circuits = ref_pauli_noise.pauli_gate_error_circuits() - noise_models = ref_pauli_noise.pauli_gate_error_noise_models() + noise_models = ref_pauli_noise.pauli_gate_error_noise_models(qerror_cls) targets = ref_pauli_noise.pauli_gate_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): @@ -417,13 +417,13 @@ def test_shot_branching_pauli_gate_noise(self, method, device): self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - @supported_methods(SUPPORTED_METHODS) - def test_shot_branching_pauli_reset_noise(self, method, device): + @supported_methods(SUPPORTED_METHODS, [noise.QuantumError, noise.PauliError]) + def test_shot_branching_pauli_reset_noise(self, method, device, qerror_cls): """Test simulation with Pauli reset error noise model.""" backend = self.backend(method=method, device=device) shots = 1000 circuits = ref_pauli_noise.pauli_reset_error_circuits() - noise_models = ref_pauli_noise.pauli_reset_error_noise_models() + noise_models = ref_pauli_noise.pauli_reset_error_noise_models(qerror_cls) targets = ref_pauli_noise.pauli_reset_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): @@ -432,13 +432,13 @@ def test_shot_branching_pauli_reset_noise(self, method, device): self.assertSuccess(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - @supported_methods(SUPPORTED_METHODS) - def test_shot_branching_pauli_measure_noise(self, method, device): + @supported_methods(SUPPORTED_METHODS, [noise.QuantumError, noise.PauliError]) + def test_shot_branching_pauli_measure_noise(self, method, device, qerror_cls): """Test simulation with Pauli measure error noise model.""" backend = self.backend(method=method, device=device) shots = 1000 circuits = ref_pauli_noise.pauli_measure_error_circuits() - noise_models = ref_pauli_noise.pauli_measure_error_noise_models() + noise_models = ref_pauli_noise.pauli_measure_error_noise_models(qerror_cls) targets = ref_pauli_noise.pauli_measure_error_counts(shots) for circuit, noise_model, target in zip(circuits, noise_models, targets): diff --git a/test/terra/reference/ref_pauli_noise.py b/test/terra/reference/ref_pauli_noise.py index c43bddfe09..ab994bd657 100644 --- a/test/terra/reference/ref_pauli_noise.py +++ b/test/terra/reference/ref_pauli_noise.py @@ -18,6 +18,7 @@ from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit from qiskit_aer.noise import NoiseModel +from qiskit_aer.noise.errors import QuantumError, PauliError from qiskit_aer.noise.errors.standard_errors import pauli_error @@ -26,6 +27,15 @@ # ========================================================================== +def _pauli_error(noise_ops, qerror_cls=QuantumError): + """For checking compatibility with `PauliError` or `pauli_error`""" + if qerror_cls == QuantumError: + return pauli_error(noise_ops) + if qerror_cls == PauliError: + return PauliError(*list(zip(*noise_ops))) + raise TypeError("Invalid Pauli channel quantum error type") + + def pauli_gate_error_circuits(): """Local Pauli gate error noise model circuits""" circuits = [] @@ -78,42 +88,42 @@ def pauli_gate_error_circuits(): return circuits -def pauli_gate_error_noise_models(): +def pauli_gate_error_noise_models(qerror_cls=QuantumError): """Local Pauli gate error noise models""" noise_models = [] # 100% all-qubit Pauli error on "id" gates - error = pauli_error([("X", 1)]) + error = _pauli_error([("X", 1)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, "id") noise_models.append(noise_model) # 25% all-qubit Pauli error on "id" gates - error = pauli_error([("X", 0.25), ("I", 0.75)]) + error = _pauli_error([("X", 0.25), ("I", 0.75)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, "id") noise_models.append(noise_model) # 100% Pauli error on "id" gates on qubit-1 - error = pauli_error([("X", 1)]) + error = _pauli_error([("X", 1)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_quantum_error(error, "id", [1]) noise_models.append(noise_model) # 25% all-qubit Pauli error on "id" gates on qubit-0 - error = pauli_error([("X", 0.25), ("I", 0.75)]) + error = _pauli_error([("X", 0.25), ("I", 0.75)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_quantum_error(error, "id", [0]) noise_models.append(noise_model) # 50% Pauli error on conditional gate that doesn't get applied - error = pauli_error([("X", 0.5), ("I", 0.5)]) + error = _pauli_error([("X", 0.5), ("I", 0.5)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, "x") noise_models.append(noise_model) # 50% Pauli error on conditional gate that does get applied - error = pauli_error([("X", 0.5), ("I", 0.5)]) + error = _pauli_error([("X", 0.5), ("I", 0.5)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, "x") noise_models.append(noise_model) @@ -177,18 +187,18 @@ def pauli_measure_error_circuits(): return circuits -def pauli_measure_error_noise_models(): +def pauli_measure_error_noise_models(qerror_cls=QuantumError): """Local Pauli measure error noise models""" noise_models = [] # 25% all-qubit Pauli error on measure - error = pauli_error([("X", 0.25), ("I", 0.75)]) + error = _pauli_error([("X", 0.25), ("I", 0.75)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, "measure") noise_models.append(noise_model) # 25% local Pauli error on measure of qubit 1 - error = pauli_error([("X", 0.25), ("I", 0.75)]) + error = _pauli_error([("X", 0.25), ("I", 0.75)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_quantum_error(error, "measure", [1]) noise_models.append(noise_model) @@ -243,18 +253,18 @@ def pauli_reset_error_circuits(): return circuits -def pauli_reset_error_noise_models(): +def pauli_reset_error_noise_models(qerror_cls=QuantumError): """Local Pauli reset error noise models""" noise_models = [] # 25% all-qubit Pauli error on reset - error = pauli_error([("X", 0.25), ("I", 0.75)]) + error = _pauli_error([("X", 0.25), ("I", 0.75)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, "reset") noise_models.append(noise_model) # 25% local Pauli error on reset of qubit 1 - error = pauli_error([("X", 0.25), ("I", 0.75)]) + error = _pauli_error([("X", 0.25), ("I", 0.75)], qerror_cls=qerror_cls) noise_model = NoiseModel() noise_model.add_quantum_error(error, "reset", [1]) noise_models.append(noise_model)