def test_measure_sampling_with_quantum_noise(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)} readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in depolarizing.items(): noise_model.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, noise_model=noise_model, backend_options=self.BACKEND_OPTS).result() self.is_completed(result) for res in result.results: self.assertIn("measure_sampling", res.metadata) self.assertEqual(res.metadata["measure_sampling"], False) # Test sampling was disabled for res in result.results: self.assertIn("measure_sampling", res.metadata) self.assertEqual(res.metadata["measure_sampling"], False)
def test_measure_sampling_with_quantum_noise(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)} readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in depolarizing.items(): noise_model.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, noise_model=noise_model, backend_options=self.BACKEND_OPTS).result() self.assertTrue(getattr(result, 'success', False)) sampling = (self.BACKEND_OPTS.get( "method", "automatic").startswith("density_matrix")) self.compare_result_metadata(result, circuits, "measure_sampling", sampling)
def noise_model(self): readout_error = [0.01, 0.1] depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)} noise = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in depolarizing.items(): noise.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) return noise
def noise_model_kraus(self): """ Creates a new noise model for testing purposes """ readout_error = [0.01, 0.1] params = {'u3': (1, 0.001), 'cx': (2, 0.02)} noise = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in params.items(): noise.add_all_qubit_quantum_error( amplitude_damping_error(gate_error, num_qubits), gate) return noise
def test_measure_sampling_with_readouterror(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result)
def test_measure_sampling_with_readouterror(self, method, device): """Test AerSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) backend = self.backend(method=method, device=device, noise_model=noise_model) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) result = backend.run(circuits, shots=shots).result() self.assertSuccess(result) self.compare_result_metadata(result, circuits, "measure_sampling", True)
def test_measure_sampling_with_quantum_noise(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)} readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in depolarizing.items(): noise_model.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, noise_model=noise_model, **self.BACKEND_OPTS).result() self.assertSuccess(result)
def test_measure_sampling_with_readouterror(self): """Test QasmSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run( qobj, noise_model=noise_model, backend_options=self.BACKEND_OPTS).result() self.is_completed(result) # Test sampling was disabled for res in result.results: self.assertIn("measure_sampling", res.metadata) self.assertEqual(res.metadata["measure_sampling"], True)
def test_measure_sampling_with_quantum_noise(self, method, device): """Test AerSimulator measure with deterministic counts with sampling and readout-error""" readout_error = [0.01, 0.1] noise_model = NoiseModel() depolarizing = {'u3': (1, 0.001), 'cx': (2, 0.02)} readout = [[1.0 - readout_error[0], readout_error[0]], [readout_error[1], 1.0 - readout_error[1]]] noise_model.add_all_qubit_readout_error(ReadoutError(readout)) for gate, (num_qubits, gate_error) in depolarizing.items(): noise_model.add_all_qubit_quantum_error( depolarizing_error(gate_error, num_qubits), gate) backend = self.backend(method=method, device=device, noise_model=noise_model) shots = 1000 circuits = ref_measure.measure_circuits_deterministic( allow_sampling=True) targets = ref_measure.measure_counts_deterministic(shots) result = backend.run(circuits, shots=shots).result() self.assertSuccess(result) sampling = (method == "density_matrix") self.compare_result_metadata(result, circuits, "measure_sampling", sampling)
import numpy as np from qiskit.providers.aer.noise import NoiseModel from qiskit.providers.aer import noise from qiskit.providers.aer.noise.errors import ReadoutError from qiskit import IBMQ # Global params NB_SHOTS = 128 # =================== # Noise models # =================== #noise in the readout (3q, p0g1 = p1g0 for the same qubit, but different over qubits) ro_errors_proba = [[0.1, 0.1],[0.05,0.05],[0.15, 0.15]] noise_ro = NoiseModel() for n, ro in enumerate(ro_errors_proba): noise_ro.add_readout_error(ReadoutError([[1 - ro[0], ro[0]], [ro[1], 1 - ro[1]]]), [n]) ro_errors_proba_sym = [[0.1, 0.1],[0.1,0.1],[0.1, 0.1]] noise_rosym = NoiseModel() for n, ro in enumerate(ro_errors_proba_sym): noise_rosym.add_readout_error(ReadoutError([[1 - ro[0], ro[0]], [ro[1], 1 - ro[1]]]), [n]) provider = IBMQ.load_account() device = provider.get_backend('ibmq_essex') properties = device.properties() noise_essex = noise.device.basic_device_noise_model(properties) # =================== # utility functions
def gen_ro_noisemodel(err_proba = [[0.1, 0.1],[0.1,0.1]], qubits=[0,1]): noise_model = NoiseModel() for ro, q in zip(err_proba, qubits): err = [[1 - ro[0], ro[0]], [ro[1], 1 - ro[1]]] noise.add_readout_error(ReadoutError(err), [q]) return noise_model
# Easy device choosing when run from file qc.CurrentStatus() chosen_device = int(input('SELECT IBM DEVICE:')) qc.GetBackend(chosen_device, inplace=True) qc.UpdateQuantumCircuit() # =================== # Noise models # =================== #noise in the readout (3q, p0g1 = p1g0 for the same qubit, but different over qubits) ro_errors_proba = [[0.1, 0.1], [0.05, 0.05], [0.15, 0.15]] noise_ro = NoiseModel() for n, ro in enumerate(ro_errors_proba): noise_ro.add_readout_error( ReadoutError([[1 - ro[0], ro[0]], [ro[1], 1 - ro[1]]]), [n]) ro_errors_proba_sym = [[0.1, 0.1], [0.1, 0.1], [0.1, 0.1]] noise_rosym = NoiseModel() for n, ro in enumerate(ro_errors_proba_sym): noise_rosym.add_readout_error( ReadoutError([[1 - ro[0], ro[0]], [ro[1], 1 - ro[1]]]), [n]) provider = qc.provider_free device = provider.get_backend('ibmq_essex') properties = device.properties() noise_essex = noise.device.basic_device_noise_model(properties) # =================== # utility functions
# ### Readout Error # # Classical readout errors are specified by a list of assignment probabilities vectors $P(A|B)$: # # * $A$ is the *recorded* classical bit value # * $B$ is the *true* bit value returned from the measurement # # E.g. for 1 qubits: $ P(A|B) = [P(A|0), P(A|1)]$. # In[8]: # Measurement miss-assignement probabilities p0given1 = 0.1 p1given0 = 0.05 ReadoutError([[1 - p1given0, p1given0], [p0given1, 1 - p0given1]]) # Readout errors may also be combined using `compose`, `tensor` and `expand` like with quantum errors. # ## Adding errors to a Noise Model # # When adding a quantum error to a noise model we must specify the type of *instruction* that it acts on, and what qubits to apply it to. There are three cases for Quantum Errors: # # 1. All-qubit quantum error # 2. Specific qubit quantum error # 3. Non-local quantum error # # ### All-qubit quantum error # # This applies the same error to any occurrence of an instruction, regardless of which qubits it acts on. #