From d58dae50383c2542ccd239a24d4e8e4baf01b7f0 Mon Sep 17 00:00:00 2001 From: "Christopher J. Wood" Date: Thu, 1 Aug 2019 00:29:45 -0400 Subject: [PATCH] Add noise tests for different qasm simulator methods (#312) * add readout error qasm noise tests * add pauli error qasm noise tests * add reset error qasm noise tests * add kraus error qasm noise tests --- .../backends/qasm_simulator/qasm_noise.py | 154 +++++ .../test_qasm_density_matrix_simulator.py | 12 +- test/terra/backends/test_qasm_simulator.py | 9 +- .../test_qasm_stabilizer_simulator.py | 9 +- .../test_qasm_statevector_simulator.py | 12 +- test/terra/noise/test_noise_model.py | 597 ------------------ test/terra/reference/ref_kraus_noise.py | 70 ++ test/terra/reference/ref_pauli_noise.py | 299 +++++++++ test/terra/reference/ref_readout_noise.py | 138 ++++ test/terra/reference/ref_reset_noise.py | 143 +++++ test/terra/utils/utils.py | 33 + 11 files changed, 872 insertions(+), 604 deletions(-) create mode 100644 test/terra/backends/qasm_simulator/qasm_noise.py create mode 100644 test/terra/reference/ref_kraus_noise.py create mode 100644 test/terra/reference/ref_pauli_noise.py create mode 100644 test/terra/reference/ref_readout_noise.py create mode 100644 test/terra/reference/ref_reset_noise.py create mode 100644 test/terra/utils/utils.py diff --git a/test/terra/backends/qasm_simulator/qasm_noise.py b/test/terra/backends/qasm_simulator/qasm_noise.py new file mode 100644 index 0000000000..7cd3c826b7 --- /dev/null +++ b/test/terra/backends/qasm_simulator/qasm_noise.py @@ -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) diff --git a/test/terra/backends/test_qasm_density_matrix_simulator.py b/test/terra/backends/test_qasm_density_matrix_simulator.py index ae1bb43b63..01af832a4f 100644 --- a/test/terra/backends/test_qasm_density_matrix_simulator.py +++ b/test/terra/backends/test_qasm_density_matrix_simulator.py @@ -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, @@ -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"} diff --git a/test/terra/backends/test_qasm_simulator.py b/test/terra/backends/test_qasm_simulator.py index 5eea2138d6..bb830862ba 100644 --- a/test/terra/backends/test_qasm_simulator.py +++ b/test/terra/backends/test_qasm_simulator.py @@ -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, @@ -66,9 +69,13 @@ class TestQasmSimulator(common.QiskitAerTestCase, QasmAlgorithmTestsWaltzBasis, QasmAlgorithmTestsMinimalBasis, QasmUnitaryGateTests, + QasmReadoutNoiseTests, + QasmPauliNoiseTests, QasmThreadManagementTests, QasmFusionTests, QasmQubitsTruncateTests, + QasmResetNoiseTests, + QasmKrausNoiseTests, QasmBasicsTests): """QasmSimulator automatic method tests.""" diff --git a/test/terra/backends/test_qasm_stabilizer_simulator.py b/test/terra/backends/test_qasm_stabilizer_simulator.py index 06b4e06db3..21c04689c5 100644 --- a/test/terra/backends/test_qasm_stabilizer_simulator.py +++ b/test/terra/backends/test_qasm_stabilizer_simulator.py @@ -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 @@ -39,7 +41,10 @@ class TestQasmStabilizerSimulator(common.QiskitAerTestCase, QasmResetTests, QasmConditionalGateTests, QasmCliffordTests, - QasmAlgorithmTests): + QasmAlgorithmTests, + QasmReadoutNoiseTests, + QasmResetNoiseTests, + QasmPauliNoiseTests): """QasmSimulator stabilizer method tests.""" BACKEND_OPTS = {"method": "stabilizer"} diff --git a/test/terra/backends/test_qasm_statevector_simulator.py b/test/terra/backends/test_qasm_statevector_simulator.py index c6d08b5105..6524c4d467 100644 --- a/test/terra/backends/test_qasm_statevector_simulator.py +++ b/test/terra/backends/test_qasm_statevector_simulator.py @@ -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 @@ -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"} diff --git a/test/terra/noise/test_noise_model.py b/test/terra/noise/test_noise_model.py index 57ad2fd5c5..09dea2366c 100644 --- a/test/terra/noise/test_noise_model.py +++ b/test/terra/noise/test_noise_model.py @@ -20,539 +20,14 @@ from qiskit.compiler import assemble, transpile from qiskit.providers.aer.backends import QasmSimulator from qiskit.providers.aer.noise import NoiseModel -from qiskit.providers.aer.noise.errors.quantum_error import QuantumError from qiskit.providers.aer.noise.errors.standard_errors import pauli_error from qiskit.providers.aer.noise.errors.standard_errors import reset_error from qiskit.providers.aer.noise.errors.standard_errors import amplitude_damping_error -from qiskit.providers.aer.utils.qobj_utils import measure_instr -from qiskit.providers.aer.utils.qobj_utils import append_instr class TestNoise(common.QiskitAerTestCase): """Testing noise model""" - def test_readout_error_qubit0(self): - """Test readout error on qubit 0 for bell state""" - - # Test circuit: ideal bell state - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]) - circuit.cx(qr[0], qr[1]) - # Ensure qubit 0 is measured before qubit 1 - circuit.barrier(qr) - circuit.measure(qr[0], cr[0]) - circuit.barrier(qr) - circuit.measure(qr[1], cr[1]) - backend = QasmSimulator() - - # Asymetric readout error on qubit-0 only - probs_given0 = [0.9, 0.1] - probs_given1 = [0.3, 0.7] - noise_model = NoiseModel() - noise_model.add_readout_error([probs_given0, probs_given1], [0]) - - shots = 2000 - target = { - '0x0': probs_given0[0] * shots / 2, - '0x1': probs_given0[1] * shots / 2, - '0x2': probs_given1[0] * shots / 2, - '0x3': probs_given1[1] * shots / 2 - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_readout_error_qubit1(self): - """Test readout error on qubit 1 for bell state""" - - # Test circuit: ideal bell state - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]) - circuit.cx(qr[0], qr[1]) - # Ensure qubit 0 is measured before qubit 1 - circuit.barrier(qr) - circuit.measure(qr[0], cr[0]) - circuit.barrier(qr) - circuit.measure(qr[1], cr[1]) - backend = QasmSimulator() - - # Asymetric readout error on qubit-0 only - probs_given0 = [0.9, 0.1] - probs_given1 = [0.3, 0.7] - noise_model = NoiseModel() - noise_model.add_readout_error([probs_given0, probs_given1], [1]) - - shots = 2000 - target = { - '0x0': probs_given0[0] * shots / 2, - '0x1': probs_given1[0] * shots / 2, - '0x2': probs_given0[1] * shots / 2, - '0x3': probs_given1[1] * shots / 2 - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_readout_error_all_qubit(self): - """Test 100% readout error on all qubits""" - - # Test circuit: ideal bell state - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.h(qr[0]) - circuit.cx(qr[0], qr[1]) - # Ensure qubit 0 is measured before qubit 1 - circuit.barrier(qr) - circuit.measure(qr[0], cr[0]) - circuit.barrier(qr) - circuit.measure(qr[1], cr[1]) - backend = QasmSimulator() - - # Asymetric readout error on qubit-0 only - probs_given0 = [0.9, 0.1] - probs_given1 = [0.3, 0.7] - noise_model = NoiseModel() - noise_model.add_all_qubit_readout_error([probs_given0, probs_given1]) - - # Expected counts - shots = 2000 - p00 = 0.5 * (probs_given0[0]**2 + probs_given1[0]**2) - p01 = 0.5 * (probs_given0[0] * probs_given0[1] + - probs_given1[0] * probs_given1[1]) - p10 = 0.5 * (probs_given0[0] * probs_given0[1] + - probs_given1[0] * probs_given1[1]) - p11 = 0.5 * (probs_given0[1]**2 + probs_given1[1]**2) - target = target = { - '0x0': p00 * shots, - '0x1': p01 * shots, - '0x2': p10 * shots, - '0x3': p11 * shots - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_readout_error_correlated_2qubit(self): - """Test a correlated two-qubit readout error""" - # Test circuit: prepare all plus state - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.h(qr) - circuit.barrier(qr) - # We will manually add a correlated measure operation to - # the assembled qobj - backend = QasmSimulator() - - # Correlated 2-qubit readout error - probs_given00 = [0.3, 0, 0, 0.7] - probs_given01 = [0, 0.6, 0.4, 0] - probs_given10 = [0, 0, 1, 0] - probs_given11 = [0.1, 0, 0, 0.9] - probs_noise = [ - probs_given00, probs_given01, probs_given10, probs_given11 - ] - noise_model = NoiseModel() - noise_model.add_readout_error(probs_noise, [0, 1]) - - # Expected counts - shots = 2000 - probs_ideal = [0.25, 0.25, 0.25, 0.25] - p00 = sum([ - ideal * noise[0] for ideal, noise in zip(probs_ideal, probs_noise) - ]) - p01 = sum([ - ideal * noise[1] for ideal, noise in zip(probs_ideal, probs_noise) - ]) - p10 = sum([ - ideal * noise[2] for ideal, noise in zip(probs_ideal, probs_noise) - ]) - p11 = sum([ - ideal * noise[3] for ideal, noise in zip(probs_ideal, probs_noise) - ]) - target = { - '0x0': p00 * shots, - '0x1': p01 * shots, - '0x2': p10 * shots, - '0x3': p11 * shots - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - # Add measure to qobj - item = measure_instr([0, 1], [0, 1]) - append_instr(qobj, 0, item) - # Execute - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_reset_error_specific_qubit_50percent(self): - """Test 50% perecent reset error on qubit-0""" - - # Test circuit: ideal outcome "11" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.x(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - noise_circs = [[{ - "name": "reset", - "qubits": [0] - }], [{ - "name": "id", - "qubits": [0] - }]] - - # 50% reset noise on qubit-0 "u3" only. - noise_probs = [0.5, 0.5] - error = QuantumError(zip(noise_circs, noise_probs)) - noise_model = NoiseModel() - noise_model.add_quantum_error(error, "u3", [0]) - shots = 2000 - target = {'0x2': shots / 2, '0x3': shots / 2} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_reset_error_specific_qubit_25percent(self): - """Test 25% percent reset error on qubit-1""" - - # Test circuit: ideal outcome "11" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.x(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - noise_circs = [[{ - "name": "reset", - "qubits": [0] - }], [{ - "name": "id", - "qubits": [0] - }]] - - # 25% reset noise on qubit-1 "u3" only. - noise_probs = [0.25, 0.75] - error = QuantumError(zip(noise_circs, noise_probs)) - noise_model = NoiseModel() - noise_model.add_quantum_error(error, "u3", [1]) - shots = 2000 - # target = {'01': shots / 4, '11': 3 * shots / 4} - target = {'0x1': shots / 4, '0x3': 3 * shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_reset_error_all_qubit_100percent(self): - """Test 100% precent reset error on all qubits""" - - # Test circuit: ideal outcome "11" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.x(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - noise_circs = [[{ - "name": "reset", - "qubits": [0] - }], [{ - "name": "id", - "qubits": [0] - }]] - - # 100% reset noise on all qubit "u3". - noise_probs = [1, 0] - error = QuantumError(zip(noise_circs, noise_probs)) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, "u3") - shots = 100 - # target = {'00': shots} - target = {'0x0': shots} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0) - - def test_all_qubit_pauli_error_gate_100percent(self): - """Test 100% Pauli error on id gates""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.iden(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 100 - # test noise model - error = pauli_error([('X', 1)]) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, 'id') - # Execute - target = {'0x3': shots} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0) - - def test_all_qubit_pauli_error_gate_25percent(self): - """Test 100% Pauli error on id gates""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.iden(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, 'id') - # Execute - target = { - '0x0': 9 * shots / 16, - '0x1': 3 * shots / 16, - '0x2': 3 * shots / 16, - '0x3': shots / 16 - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_specific_qubit_pauli_error_gate_100percent(self): - """Test 100% Pauli error on id gates on qubit-1""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.iden(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 100 - # test noise model - error = pauli_error([('X', 1)]) - noise_model = NoiseModel() - noise_model.add_quantum_error(error, 'id', [1]) - # Execute - target = {'0x2': shots} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0) - - def test_specific_qubit_pauli_error_gate_25percent(self): - """Test 100% Pauli error on id gates qubit-0""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.iden(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_quantum_error(error, 'id', [0]) - # Execute - target = {'0x0': 3 * shots / 4, '0x1': shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_nonlocal_pauli_error_gate_25percent(self): - """Test 100% non-local Pauli error on cx(0, 1) gate""" - qr = QuantumRegister(3, 'qr') - cr = ClassicalRegister(3, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.cx(qr[0], qr[1]) - circuit.barrier(qr) - circuit.cx(qr[1], qr[0]) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('XII', 0.25), ('III', 0.75)]) - noise_model = NoiseModel() - noise_model.add_nonlocal_quantum_error(error, 'cx', [0, 1], [0, 1, 2]) - # Execute - target = {'0x0': 3 * shots / 4, '0x4': shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_all_qubit_pauli_error_measure_25percent(self): - """Test 25% Pauli-X error on measure""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, 'measure') - # Execute - target = { - '0x0': 9 * shots / 16, - '0x1': 3 * shots / 16, - '0x2': 3 * shots / 16, - '0x3': shots / 16 - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_specific_qubit_pauli_error_measure_25percent(self): - """Test 25% Pauli-X error on measure of qubit-1""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_quantum_error(error, 'measure', [1]) - # Execute - target = {'0x0': 3 * shots / 4, '0x2': shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_nonlocal_pauli_error_measure_25percent(self): - """Test 25% Pauli-X error on qubit-1 when measuring qubit 0""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - # use barrier to ensure measure qubit 0 is before qubit 1 - circuit.measure(qr[0], cr[0]) - circuit.barrier(qr) - circuit.measure(qr[1], cr[1]) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_nonlocal_quantum_error(error, 'measure', [0], [1]) - # Execute - target = {'0x0': 3 * shots / 4, '0x2': shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_all_qubit_pauli_error_reset_25percent(self): - """Test 25% Pauli-X error on reset""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.barrier(qr) - circuit.reset(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, 'reset') - # Execute - target = { - '0x0': 9 * shots / 16, - '0x1': 3 * shots / 16, - '0x2': 3 * shots / 16, - '0x3': shots / 16 - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_specific_qubit_pauli_error_reset_25percent(self): - """Test 25% Pauli-X error on reset of qubit-1""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.barrier(qr) - circuit.reset(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_quantum_error(error, 'reset', [1]) - # Execute - target = {'0x0': 3 * shots / 4, '0x2': shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - - def test_nonlocal_pauli_error_reset_25percent(self): - """Test 25% Pauli-X error on qubit-1 when reseting qubit 0""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.barrier(qr) - circuit.reset(qr[1]) - circuit.barrier(qr) - circuit.reset(qr[0]) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = pauli_error([('X', 0.25), ('I', 0.75)]) - noise_model = NoiseModel() - noise_model.add_nonlocal_quantum_error(error, 'reset', [0], [1]) - # Execute - target = {'0x0': 3 * shots / 4, '0x2': shots / 4} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - def test_amplitude_damping_error(self): """Test amplitude damping error damps to correct state""" qr = QuantumRegister(1, 'qr') @@ -579,78 +54,6 @@ def test_amplitude_damping_error(self): self.is_completed(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - def test_standard_reset0_error_100percent(self): - """Test 100% Pauli error on id gates""" - qr = QuantumRegister(1, 'qr') - cr = ClassicalRegister(1, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.x(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 100 - # test noise model - error = reset_error(1) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) - # Execute - target = {'0x0': shots} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0) - - def test_standard_reset1_error_100percent(self): - """Test 100% Pauli error on id gates""" - qr = QuantumRegister(1, 'qr') - cr = ClassicalRegister(1, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.iden(qr) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 100 - # test noise model - error = reset_error(0, 1) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) - # Execute - target = {'0x1': shots} - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0) - - def test_standard_reset0reset1_error_50percent(self): - """Test 100% Pauli error on id gates""" - qr = QuantumRegister(2, 'qr') - cr = ClassicalRegister(2, 'cr') - circuit = QuantumCircuit(qr, cr) - circuit.iden(qr[0]) - circuit.x(qr[1]) - circuit.barrier(qr) - circuit.measure(qr, cr) - backend = QasmSimulator() - shots = 2000 - # test noise model - error = reset_error(0.25, 0.25) - noise_model = NoiseModel() - noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) - # Execute - target = { - '0x0': 3 * shots / 16, - '0x1': shots / 16, - '0x2': 9 * shots / 16, - '0x3': 3 * shots / 16 - } - circuit = transpile(circuit, basis_gates=noise_model.basis_gates) - qobj = assemble([circuit], backend, shots=shots) - result = backend.run(qobj, noise_model=noise_model).result() - self.is_completed(result) - self.compare_counts(result, [circuit], [target], delta=0.05 * shots) - def test_noise_model_basis_gates(self): """Test noise model basis_gates""" basis_gates = ['u1', 'u2', 'u3', 'cx'] diff --git a/test/terra/reference/ref_kraus_noise.py b/test/terra/reference/ref_kraus_noise.py new file mode 100644 index 0000000000..1c9cc087ea --- /dev/null +++ b/test/terra/reference/ref_kraus_noise.py @@ -0,0 +1,70 @@ +# 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 kraus error NoiseModel integration tests +""" + +from test.terra.utils.utils import list2dict + +from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit +from qiskit.providers.aer.noise import NoiseModel +from qiskit.providers.aer.noise.errors.standard_errors import amplitude_damping_error + + +# ========================================================================== +# Amplitude damping error +# ========================================================================== + +def kraus_gate_error_circuits(): + """Kraus gate error noise model circuits""" + circuits = [] + + # Repeated amplitude damping to diagonal state + qr = QuantumRegister(1, 'qr') + cr = ClassicalRegister(1, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.x(qr) # prepare + state + for _ in range(30): + # Add noisy identities + circuit.barrier(qr) + circuit.iden(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + return circuits + + +def kraus_gate_error_noise_models(): + """Kraus gate error noise models""" + noise_models = [] + + # Amplitude damping error on "id" + error = amplitude_damping_error(0.75, 0.25) + noise_model = NoiseModel() + noise_model.add_all_qubit_quantum_error(error, 'id') + noise_models.append(noise_model) + + return noise_models + + +def kraus_gate_error_counts(shots, hex_counts=True): + """Kraus gate error circuits reference counts""" + counts_lists = [] + + # 100% all-qubit Pauli error on "id" gates + counts = [3 * shots / 4, shots / 4, 0, 0] + counts_lists.append(counts) + + # Convert to counts dict + return [list2dict(i, hex_counts) for i in counts_lists] diff --git a/test/terra/reference/ref_pauli_noise.py b/test/terra/reference/ref_pauli_noise.py new file mode 100644 index 0000000000..1527ca152d --- /dev/null +++ b/test/terra/reference/ref_pauli_noise.py @@ -0,0 +1,299 @@ +# 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 readout error NoiseModel integration tests +""" + +from test.terra.utils.utils import list2dict + +from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit +from qiskit.providers.aer.noise import NoiseModel +from qiskit.providers.aer.noise.errors.standard_errors import pauli_error + + +# ========================================================================== +# Pauli Gate Errors +# ========================================================================== + +def pauli_gate_error_circuits(): + """Local Pauli gate error noise model circuits""" + circuits = [] + + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + + # 100% all-qubit Pauli error on "id" gate + circuit = QuantumCircuit(qr, cr) + circuit.iden(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% all-qubit Pauli error on "id" gates + circuit = QuantumCircuit(qr, cr) + circuit.iden(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 100% Pauli error on "id" gates on qubit-1 + circuit = QuantumCircuit(qr, cr) + circuit.iden(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% all-qubit Pauli error on "id" gates on qubit-0 + circuit = QuantumCircuit(qr, cr) + circuit.iden(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% Pauli-X error on spectator for CX gate on [0, 1] + qr = QuantumRegister(3, 'qr') + cr = ClassicalRegister(3, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.cx(qr[0], qr[1]) + circuit.barrier(qr) + circuit.cx(qr[1], qr[0]) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + return circuits + + +def pauli_gate_error_noise_models(): + """Local Pauli gate error noise models""" + noise_models = [] + + # 100% all-qubit Pauli error on "id" gates + error = pauli_error([('X', 1)]) + 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)]) + 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)]) + 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)]) + noise_model = NoiseModel() + noise_model.add_quantum_error(error, 'id', [0]) + noise_models.append(noise_model) + + # 25% Pauli-X error on spectator for CX gate on [0, 1] + error = pauli_error([('XII', 0.25), ('III', 0.75)]) + noise_model = NoiseModel() + noise_model.add_nonlocal_quantum_error(error, 'cx', [0, 1], [0, 1, 2]) + noise_models.append(noise_model) + + return noise_models + + +def pauli_gate_error_counts(shots, hex_counts=True): + """Pauli gate error circuits reference counts""" + counts_lists = [] + + # 100% all-qubit Pauli error on "id" gates + counts = [0, 0, 0, shots] + counts_lists.append(counts) + + # 25% all-qubit Pauli error on "id" gates + counts = [9 * shots / 16, 3 * shots / 16, 3 * shots / 16, shots / 16] + counts_lists.append(counts) + + # 100% Pauli error on "id" gates on qubit-1 + counts = [0, 0, shots, 0] + counts_lists.append(counts) + + # 25% all-qubit Pauli error on "id" gates on qubit-0 + counts = [3 * shots / 4, shots / 4, 0, 0] + counts_lists.append(counts) + + # 25% Pauli-X error on spectator for CX gate on [0, 1] + counts = [3 * shots / 4, 0, 0, 0, shots / 4, 0, 0, 0] + counts_lists.append(counts) + + # Convert to counts dict + return [list2dict(i, hex_counts) for i in counts_lists] + + +# ========================================================================== +# Pauli Measure Errors +# ========================================================================== + +def pauli_measure_error_circuits(): + """Local Pauli measure error noise model circuits""" + circuits = [] + + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + + # 25% all-qubit Pauli error on measure + circuit = QuantumCircuit(qr, cr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% local Pauli error on measure of qubit 1 + circuit = QuantumCircuit(qr, cr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25 % non-local Pauli error on qubit 1 for measure of qubit-1 + circuit = QuantumCircuit(qr, cr) + circuit.measure(qr, cr) + circuits.append(circuit) + + return circuits + + +def pauli_measure_error_noise_models(): + """Local Pauli measure error noise models""" + noise_models = [] + + # 25% all-qubit Pauli error on measure + error = pauli_error([('X', 0.25), ('I', 0.75)]) + 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)]) + noise_model = NoiseModel() + noise_model.add_quantum_error(error, 'measure', [1]) + noise_models.append(noise_model) + + # 25 % non-local Pauli error on qubit 1 for measure of qubit-1 + error = pauli_error([('X', 0.25), ('I', 0.75)]) + noise_model = NoiseModel() + noise_model.add_nonlocal_quantum_error(error, 'measure', [0], [1]) + noise_models.append(noise_model) + + return noise_models + + +def pauli_measure_error_counts(shots, hex_counts=True): + """Local Pauli measure error circuits reference counts""" + counts_lists = [] + + # 25% all-qubit Pauli error on measure + counts = [9 * shots / 16, 3 * shots / 16, 3 * shots / 16, shots / 16] + counts_lists.append(counts) + + # 25% local Pauli error on measure of qubit 1 + counts = [3 * shots / 4, 0, shots / 4, 0] + counts_lists.append(counts) + + # 25 % non-local Pauli error on qubit 1 for measure of qubit-1 + counts = [3 * shots / 4, 0, shots / 4, 0] + counts_lists.append(counts) + + # Convert to counts dict + return [list2dict(i, hex_counts) for i in counts_lists] + + +# ========================================================================== +# Pauli Reset Errors +# ========================================================================== + + +def pauli_reset_error_circuits(): + """Local Pauli reset error noise model circuits""" + circuits = [] + + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + + # 25% all-qubit Pauli error on reset + circuit = QuantumCircuit(qr, cr) + circuit.barrier(qr) + circuit.reset(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% local Pauli error on reset of qubit 1 + circuit = QuantumCircuit(qr, cr) + circuit.barrier(qr) + circuit.reset(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25 % non-local Pauli error on qubit 1 for reset of qubit-0 + circuit = QuantumCircuit(qr, cr) + circuit.barrier(qr) + circuit.reset(qr[1]) + circuit.barrier(qr) + circuit.reset(qr[0]) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + return circuits + + +def pauli_reset_error_noise_models(): + """Local Pauli reset error noise models""" + noise_models = [] + + # 25% all-qubit Pauli error on reset + error = pauli_error([('X', 0.25), ('I', 0.75)]) + 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)]) + noise_model = NoiseModel() + noise_model.add_quantum_error(error, 'reset', [1]) + noise_models.append(noise_model) + + # 25 % non-local Pauli error on qubit 1 for reset of qubit-0 + error = pauli_error([('X', 0.25), ('I', 0.75)]) + noise_model = NoiseModel() + noise_model.add_nonlocal_quantum_error(error, 'reset', [0], [1]) + noise_models.append(noise_model) + + return noise_models + + +def pauli_reset_error_counts(shots, hex_counts=True): + """Local Pauli reset error circuits reference counts""" + counts_lists = [] + + # 25% all-qubit Pauli error on reset + counts = [9 * shots / 16, 3 * shots / 16, 3 * shots / 16, shots / 16] + counts_lists.append(counts) + + # 25% local Pauli error on reset of qubit 1 + counts = [3 * shots / 4, 0, shots / 4, 0] + counts_lists.append(counts) + + # 25 % non-local Pauli error on qubit 1 for reset of qubit-0 + counts = [3 * shots / 4, 0, shots / 4, 0] + counts_lists.append(counts) + + # Convert to counts dict + return [list2dict(i, hex_counts) for i in counts_lists] diff --git a/test/terra/reference/ref_readout_noise.py b/test/terra/reference/ref_readout_noise.py new file mode 100644 index 0000000000..2b5e4bf410 --- /dev/null +++ b/test/terra/reference/ref_readout_noise.py @@ -0,0 +1,138 @@ +# 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 readout error NoiseModel integration tests +""" + +from test.terra.utils.utils import list2dict + +from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit +from qiskit.circuit import Instruction +from qiskit.providers.aer.noise import NoiseModel + + +# ========================================================================== +# Readout error +# ========================================================================== + +# Error matrices used in tests +ROERROR_1Q = [[0.9, 0.1], [0.3, 0.7]] +ROERROR_2Q = [[0.3, 0, 0, 0.7], [0, 0.6, 0.4, 0], [0, 0, 1, 0], [0.1, 0, 0, 0.9]] + + +def readout_error_circuits(): + """Readout error test circuits""" + + circuits = [] + + # Test circuit: ideal bell state for 1-qubit readout errors + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.h(qr[0]) + circuit.cx(qr[0], qr[1]) + # Ensure qubit 0 is measured before qubit 1 + circuit.barrier(qr) + circuit.measure(qr[0], cr[0]) + circuit.barrier(qr) + circuit.measure(qr[1], cr[1]) + + # Add three copies of circuit + circuits += 3 * [circuit] + + # 2-qubit correlated readout error circuit + measure2 = Instruction("measure", 2, 2, []) # 2-qubit measure + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.h(qr) + circuit.barrier(qr) + circuit.append(measure2, [0, 1], [0, 1]) + + circuits.append(circuit) + + return circuits + + +def readout_error_noise_models(): + """Readout error test circuit noise models.""" + noise_models = [] + + # 1-qubit readout error on qubit 0 + noise_model = NoiseModel() + noise_model.add_readout_error(ROERROR_1Q, [0]) + noise_models.append(noise_model) + + # 1-qubit readout error on qubit 1 + noise_model = NoiseModel() + noise_model.add_readout_error(ROERROR_1Q, [1]) + noise_models.append(noise_model) + + # 1-qubit readout error on qubit 1 + noise_model = NoiseModel() + noise_model.add_all_qubit_readout_error(ROERROR_1Q) + noise_models.append(noise_model) + + # 2-qubit readout error on qubits 0,1 + noise_model = NoiseModel() + noise_model.add_readout_error(ROERROR_2Q, [0, 1]) + noise_models.append(noise_model) + + return noise_models + + +def readout_error_counts(shots, hex_counts=True): + """Readout error test circuits reference counts.""" + counts_lists = [] + + # 1-qubit readout error on qubit 0 + counts = [ + ROERROR_1Q[0][0] * shots / 2, ROERROR_1Q[0][1] * shots / 2, + ROERROR_1Q[1][0] * shots / 2, ROERROR_1Q[1][1] * shots / 2 + ] + counts_lists.append(counts) + + # 1-qubit readout error on qubit 1 + counts = [ + ROERROR_1Q[0][0] * shots / 2, ROERROR_1Q[1][0] * shots / 2, + ROERROR_1Q[0][1] * shots / 2, ROERROR_1Q[1][1] * shots / 2 + ] + counts_lists.append(counts) + + # 1-qubit readout error on qubit 1 + p00 = 0.5 * (ROERROR_1Q[0][0]**2 + ROERROR_1Q[1][0]**2) + p01 = 0.5 * ( + ROERROR_1Q[0][0] * ROERROR_1Q[0][1] + ROERROR_1Q[1][0] * ROERROR_1Q[1][1]) + p10 = 0.5 * ( + ROERROR_1Q[0][0] * ROERROR_1Q[0][1] + ROERROR_1Q[1][0] * ROERROR_1Q[1][1]) + p11 = 0.5 * (ROERROR_1Q[0][1]**2 + ROERROR_1Q[1][1]**2) + counts = [p00 * shots, p01 * shots, p10 * shots, p11 * shots] + counts_lists.append(counts) + + # 2-qubit readout error on qubits 0,1 + probs_ideal = [0.25, 0.25, 0.25, 0.25] + p00 = sum([ + ideal * noise[0] for ideal, noise in zip(probs_ideal, ROERROR_2Q) + ]) + p01 = sum([ + ideal * noise[1] for ideal, noise in zip(probs_ideal, ROERROR_2Q) + ]) + p10 = sum([ + ideal * noise[2] for ideal, noise in zip(probs_ideal, ROERROR_2Q) + ]) + p11 = sum([ + ideal * noise[3] for ideal, noise in zip(probs_ideal, ROERROR_2Q) + ]) + counts = [p00 * shots, p01 * shots, p10 * shots, p11 * shots] + counts_lists.append(counts) + + return [list2dict(i, hex_counts) for i in counts_lists] diff --git a/test/terra/reference/ref_reset_noise.py b/test/terra/reference/ref_reset_noise.py new file mode 100644 index 0000000000..b83936e154 --- /dev/null +++ b/test/terra/reference/ref_reset_noise.py @@ -0,0 +1,143 @@ +# 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 reset error NoiseModel integration tests +""" + +from test.terra.utils.utils import list2dict + +from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit +from qiskit.providers.aer.noise import NoiseModel +from qiskit.providers.aer.noise.errors.standard_errors import reset_error + + +# ========================================================================== +# Reset Gate Errors +# ========================================================================== + +def reset_gate_error_circuits(): + """Reset gate error noise model circuits""" + circuits = [] + + # 50% reset to 0 state on qubit 0 + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.x(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% reset to 0 state on qubit 1 + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.x(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 100% reset error to 0 on all qubits + qr = QuantumRegister(1, 'qr') + cr = ClassicalRegister(1, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.x(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 100% reset error to 1 on all qubits + qr = QuantumRegister(1, 'qr') + cr = ClassicalRegister(1, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.iden(qr) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + # 25% reset error to 0 and 1 on all qubits + qr = QuantumRegister(2, 'qr') + cr = ClassicalRegister(2, 'cr') + circuit = QuantumCircuit(qr, cr) + circuit.iden(qr[0]) + circuit.x(qr[1]) + circuit.barrier(qr) + circuit.measure(qr, cr) + circuits.append(circuit) + + return circuits + + +def reset_gate_error_noise_models(): + """Reset gate error noise models""" + noise_models = [] + + # 50% reset to 0 state on qubit 0 + error = reset_error(0.5) + noise_model = NoiseModel() + noise_model.add_quantum_error(error, 'x', [0]) + noise_models.append(noise_model) + + # 25% reset to 0 state on qubit 1 + error = reset_error(0.25) + noise_model = NoiseModel() + noise_model.add_quantum_error(error, 'x', [1]) + noise_models.append(noise_model) + + # 100% reset error to 0 on all qubits + error = reset_error(1) + noise_model = NoiseModel() + noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) + noise_models.append(noise_model) + + # 100% reset error to 1 on all qubits + error = reset_error(0, 1) + noise_model = NoiseModel() + noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) + noise_models.append(noise_model) + + # 25% reset error to 0 and 1 on all qubits + error = reset_error(0.25, 0.25) + noise_model = NoiseModel() + noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) + noise_models.append(noise_model) + + return noise_models + + +def reset_gate_error_counts(shots, hex_counts=True): + """Reset gate error circuits reference counts""" + counts_lists = [] + + # 50% reset to 0 state on qubit 0 + counts = [0, 0, shots / 2, shots / 2] + counts_lists.append(counts) + + # 25% reset to 0 state on qubit 1 + counts = [0, shots / 4, 0, 3 * shots / 4] + counts_lists.append(counts) + + # 100% reset error to 0 on all qubits + counts = [shots, 0, 0, 0] + counts_lists.append(counts) + + # 100% reset error to 1 on all qubits + counts = [0, shots, 0, 0] + counts_lists.append(counts) + + # 25% reset error to 0 and 1 on all qubits + counts = [3 * shots / 16, shots / 16, 9 * shots / 16, 3 * shots / 16] + counts_lists.append(counts) + + # Convert to counts dict + return [list2dict(i, hex_counts) for i in counts_lists] diff --git a/test/terra/utils/utils.py b/test/terra/utils/utils.py new file mode 100644 index 0000000000..219a987171 --- /dev/null +++ b/test/terra/utils/utils.py @@ -0,0 +1,33 @@ +# 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. + +""" +Utils +""" + +from math import log2 + + +def list2dict(counts_list, hex_counts=True): + """Convert a list of counts to a dict""" + if hex_counts: + return {hex(i): val for i, val in enumerate(counts_list) if val > 0} + # For bit-string counts we need to know number of qubits to + # pad bitstring + n_qubits = int(log2(counts_list)) + counts_dict = {} + for i, val in enumerate(counts_list): + if val > 0: + key = bin(i)[2:] + key = (n_qubits - len(key)) * '0' + key + counts_dict[key] = val + return counts_dict