Skip to content

Commit

Permalink
Add noise tests for different qasm simulator methods (#312)
Browse files Browse the repository at this point in the history
* add readout error qasm noise tests

* add pauli error qasm noise tests

* add reset error qasm noise tests

* add kraus error qasm noise tests
  • Loading branch information
chriseclectic authored Aug 1, 2019
1 parent 7a6acf8 commit d58dae5
Show file tree
Hide file tree
Showing 11 changed files with 872 additions and 604 deletions.
154 changes: 154 additions & 0 deletions test/terra/backends/qasm_simulator/qasm_noise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2019.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""
QasmSimulator Integration Tests
"""

from test.terra.reference import ref_readout_noise
from test.terra.reference import ref_pauli_noise
from test.terra.reference import ref_reset_noise
from test.terra.reference import ref_kraus_noise

from qiskit.compiler import assemble
from qiskit.providers.aer import QasmSimulator


class QasmReadoutNoiseTests:
"""QasmSimulator readout error noise model tests."""

SIMULATOR = QasmSimulator()
BACKEND_OPTS = {}

def test_readout_noise(self):
"""Test simulation with classical readout error noise model."""
# For statevector output we can combine deterministic and non-deterministic
# count output circuits
shots = 2000
circuits = ref_readout_noise.readout_error_circuits()
noise_models = ref_readout_noise.readout_error_noise_models()
targets = ref_readout_noise.readout_error_counts(shots)

for circuit, noise_model, target in zip(circuits, noise_models,
targets):
qobj = assemble(circuit, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(
qobj,
backend_options=self.BACKEND_OPTS,
noise_model=noise_model).result()
self.is_completed(result)
self.compare_counts(result, [circuit], [target], delta=0.05 * shots)


class QasmPauliNoiseTests:
"""QasmSimulator pauli error noise model tests."""

SIMULATOR = QasmSimulator()
BACKEND_OPTS = {}

def test_pauli_gate_noise(self):
"""Test simulation with Pauli gate error noise model."""
shots = 2000
circuits = ref_pauli_noise.pauli_gate_error_circuits()
noise_models = ref_pauli_noise.pauli_gate_error_noise_models()
targets = ref_pauli_noise.pauli_gate_error_counts(shots)

for circuit, noise_model, target in zip(circuits, noise_models,
targets):
qobj = assemble(circuit, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(
qobj,
backend_options=self.BACKEND_OPTS,
noise_model=noise_model).result()
self.is_completed(result)
self.compare_counts(result, [circuit], [target], delta=0.05 * shots)

def test_pauli_reset_noise(self):
"""Test simulation with Pauli reset error noise model."""
shots = 2000
circuits = ref_pauli_noise.pauli_reset_error_circuits()
noise_models = ref_pauli_noise.pauli_reset_error_noise_models()
targets = ref_pauli_noise.pauli_reset_error_counts(shots)

for circuit, noise_model, target in zip(circuits, noise_models,
targets):
qobj = assemble(circuit, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(
qobj,
backend_options=self.BACKEND_OPTS,
noise_model=noise_model).result()
self.is_completed(result)
self.compare_counts(result, [circuit], [target], delta=0.05 * shots)

def test_pauli_measure_noise(self):
"""Test simulation with Pauli measure error noise model."""
shots = 2000
circuits = ref_pauli_noise.pauli_measure_error_circuits()
noise_models = ref_pauli_noise.pauli_measure_error_noise_models()
targets = ref_pauli_noise.pauli_measure_error_counts(shots)

for circuit, noise_model, target in zip(circuits, noise_models,
targets):
qobj = assemble(circuit, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(
qobj,
backend_options=self.BACKEND_OPTS,
noise_model=noise_model).result()
self.is_completed(result)
self.compare_counts(result, [circuit], [target], delta=0.05 * shots)


class QasmResetNoiseTests:
"""QasmSimulator reset error noise model tests."""

SIMULATOR = QasmSimulator()
BACKEND_OPTS = {}

def test_reset_gate_noise(self):
"""Test simulation with reset gate error noise model."""
shots = 2000
circuits = ref_reset_noise.reset_gate_error_circuits()
noise_models = ref_reset_noise.reset_gate_error_noise_models()
targets = ref_reset_noise.reset_gate_error_counts(shots)

for circuit, noise_model, target in zip(circuits, noise_models,
targets):
qobj = assemble(circuit, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(
qobj,
backend_options=self.BACKEND_OPTS,
noise_model=noise_model).result()
self.is_completed(result)
self.compare_counts(result, [circuit], [target], delta=0.05 * shots)


class QasmKrausNoiseTests:
"""QasmSimulator Kraus error noise model tests."""

SIMULATOR = QasmSimulator()
BACKEND_OPTS = {}

def test_kraus_gate_noise(self):
"""Test simulation with Kraus gate error noise model."""
shots = 2000
circuits = ref_kraus_noise.kraus_gate_error_circuits()
noise_models = ref_kraus_noise.kraus_gate_error_noise_models()
targets = ref_kraus_noise.kraus_gate_error_counts(shots)

for circuit, noise_model, target in zip(circuits, noise_models,
targets):
qobj = assemble(circuit, self.SIMULATOR, shots=shots)
result = self.SIMULATOR.run(
qobj,
backend_options=self.BACKEND_OPTS,
noise_model=noise_model).result()
self.is_completed(result)
self.compare_counts(result, [circuit], [target], delta=0.05 * shots)
12 changes: 10 additions & 2 deletions test/terra/backends/test_qasm_density_matrix_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTestsWaltzBasis
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTestsMinimalBasis
# Noise model simulation tests
# TODO
from test.terra.backends.qasm_simulator.qasm_noise import QasmReadoutNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmPauliNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmResetNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmKrausNoiseTests



class TestQasmDensityMatrixSimulator(common.QiskitAerTestCase,
Expand All @@ -58,7 +62,11 @@ class TestQasmDensityMatrixSimulator(common.QiskitAerTestCase,
QasmAlgorithmTests,
QasmAlgorithmTestsWaltzBasis,
QasmAlgorithmTestsMinimalBasis,
QasmUnitaryGateTests):
QasmUnitaryGateTests,
QasmReadoutNoiseTests,
QasmPauliNoiseTests,
QasmResetNoiseTests,
QasmKrausNoiseTests):
"""QasmSimulator density_matrix method tests."""

BACKEND_OPTS = {"method": "density_matrix"}
Expand Down
9 changes: 8 additions & 1 deletion test/terra/backends/test_qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTestsWaltzBasis
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTestsMinimalBasis
# Noise model simulation tests
# TODO
from test.terra.backends.qasm_simulator.qasm_noise import QasmReadoutNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmPauliNoiseTests
# Other tests
from test.terra.backends.qasm_simulator.qasm_method import QasmMethodTests
from test.terra.backends.qasm_simulator.qasm_thread_management import QasmThreadManagementTests
from test.terra.backends.qasm_simulator.qasm_fusion import QasmFusionTests
from test.terra.backends.qasm_simulator.qasm_truncate import QasmQubitsTruncateTests
from test.terra.backends.qasm_simulator.qasm_basics import QasmBasicsTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmResetNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmKrausNoiseTests


class TestQasmSimulator(common.QiskitAerTestCase,
Expand All @@ -66,9 +69,13 @@ class TestQasmSimulator(common.QiskitAerTestCase,
QasmAlgorithmTestsWaltzBasis,
QasmAlgorithmTestsMinimalBasis,
QasmUnitaryGateTests,
QasmReadoutNoiseTests,
QasmPauliNoiseTests,
QasmThreadManagementTests,
QasmFusionTests,
QasmQubitsTruncateTests,
QasmResetNoiseTests,
QasmKrausNoiseTests,
QasmBasicsTests):
"""QasmSimulator automatic method tests."""

Expand Down
9 changes: 7 additions & 2 deletions test/terra/backends/test_qasm_stabilizer_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
# Algorithm circuit tests
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTests
# Noise model simulation tests
# TODO
from test.terra.backends.qasm_simulator.qasm_noise import QasmReadoutNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmPauliNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmResetNoiseTests
# Other tests
from test.terra.backends.qasm_simulator.qasm_method import QasmMethodTests

Expand All @@ -39,7 +41,10 @@ class TestQasmStabilizerSimulator(common.QiskitAerTestCase,
QasmResetTests,
QasmConditionalGateTests,
QasmCliffordTests,
QasmAlgorithmTests):
QasmAlgorithmTests,
QasmReadoutNoiseTests,
QasmResetNoiseTests,
QasmPauliNoiseTests):
"""QasmSimulator stabilizer method tests."""

BACKEND_OPTS = {"method": "stabilizer"}
Expand Down
12 changes: 10 additions & 2 deletions test/terra/backends/test_qasm_statevector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTestsWaltzBasis
from test.terra.backends.qasm_simulator.qasm_algorithms import QasmAlgorithmTestsMinimalBasis
# Noise model simulation tests
# TODO

from test.terra.backends.qasm_simulator.qasm_noise import QasmReadoutNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmPauliNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmResetNoiseTests
from test.terra.backends.qasm_simulator.qasm_noise import QasmKrausNoiseTests
# Other tests
from test.terra.backends.qasm_simulator.qasm_method import QasmMethodTests

Expand All @@ -60,7 +64,11 @@ class TestQasmStatevectorSimulator(common.QiskitAerTestCase,
QasmAlgorithmTests,
QasmAlgorithmTestsWaltzBasis,
QasmAlgorithmTestsMinimalBasis,
QasmUnitaryGateTests):
QasmUnitaryGateTests,
QasmReadoutNoiseTests,
QasmPauliNoiseTests,
QasmResetNoiseTests,
QasmKrausNoiseTests):
"""QasmSimulator statevector method tests."""

BACKEND_OPTS = {"method": "statevector"}
Expand Down
Loading

0 comments on commit d58dae5

Please sign in to comment.