Skip to content

Commit

Permalink
Add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Jun 3, 2024
1 parent d3b8d2c commit 3bbafe0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
19 changes: 10 additions & 9 deletions test/terra/backends/aer_simulator/test_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
18 changes: 9 additions & 9 deletions test/terra/backends/aer_simulator/test_shot_branching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
36 changes: 23 additions & 13 deletions test/terra/reference/ref_pauli_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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 = []
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3bbafe0

Please sign in to comment.