def _u3fix(self, qc): nst = 2**len(self.mes_qbt) bins = [format(i, '0%db' % len(self.mes_qbt)) for i in range(nst)] # ideal result of this circuit ideal = execute(qc, backend=QASM, shots=self.shots * 10) idealcounts = ideal.result().get_counts() idealst = np.array( [idealcounts.get(i, 0) / (self.shots * 10) for i in bins]) print(idealst) # making noise model with error rate u3error = depolarizing_error(self.one_error, 1) # start simulations mean_fid = [] std_fid = [] for two_err in tqdm(self.two_error): mid = [] noise_model = NoiseModel() cxerror = depolarizing_error(two_err, 2) noise_model.add_all_qubit_quantum_error(u3error, ['u3']) noise_model.add_all_qubit_quantum_error(cxerror, ['cx']) for t in range(self.extime): # execute! job = execute(qc, backend=QASM, noise_model=noise_model, shots=self.shots) counts = job.result().get_counts() stvec = [counts.get(i, 0) / self.shots for i in bins] stf = state_fidelity(idealst, stvec) mid.append(stf) print(stvec) mean_fid.append(np.mean(mid)) std_fid.append(np.std(mid)) return mean_fid, std_fid
def test_reduce_noise_model(self): """Test reduction mapping of noise model.""" error1 = depolarizing_error(0.5, 1) error2 = depolarizing_error(0.5, 2) roerror1 = [[0.9, 0.1], [0.5, 0.5]] roerror2 = [[0.8, 0.2, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0.1, 0.9]] model = NoiseModel() model.add_all_qubit_quantum_error(error1, ['u3'], False) model.add_quantum_error(error1, ['u3'], [1], False) with self.assertWarns(DeprecationWarning): model.add_nonlocal_quantum_error(error2, ['cx'], [2, 0], [3, 1], False) model.add_all_qubit_readout_error(roerror1, False) model.add_readout_error(roerror2, [0, 2], False) remapped_model = remap_noise_model(model, [0, 1, 2], discard_qubits=True, warnings=False) target = NoiseModel() target.add_all_qubit_quantum_error(error1, ['u3'], False) target.add_quantum_error(error1, ['u3'], [1], False) target.add_all_qubit_readout_error(roerror1, False) target.add_readout_error(roerror2, [0, 2], False) self.assertEqual(remapped_model, target)
def mixed_unitary_noise_model(): """Return test rest mixed unitary noise model""" noise_model = NoiseModel() error1 = depolarizing_error(0.1, 1) noise_model.add_all_qubit_quantum_error(error1, ['u1', 'u2', 'u3']) error2 = depolarizing_error(0.1, 2) noise_model.add_all_qubit_quantum_error(error2, ['cx']) return NoiseWithDescription(noise_model, "Mixed Unitary Noise")
def test_remap_all_qubit_quantum_errors(self): """Test remapper doesn't effect all-qubit quantum errors.""" model = NoiseModel() error1 = depolarizing_error(0.5, 1) error2 = depolarizing_error(0.5, 2) model.add_all_qubit_quantum_error(error1, ['u3'], False) model.add_all_qubit_quantum_error(error2, ['cx'], False) remapped_model = remap_noise_model(model, [[0, 1], [1, 0]], warnings=False) self.assertEqual(model, remapped_model)
def test_remap_nonlocal_quantum_errors(self): """Test remapping of non-local quantum errors.""" model = NoiseModel() error1 = depolarizing_error(0.5, 1) error2 = depolarizing_error(0.5, 2) model.add_nonlocal_quantum_error(error1, ['u3'], [0], [1], False) model.add_nonlocal_quantum_error(error2, ['cx'], [1, 2], [3, 0], False) remapped_model = remap_noise_model(model, [[0, 1], [1, 2], [2, 0]], warnings=False) target = NoiseModel() target.add_nonlocal_quantum_error(error1, ['u3'], [1], [2], False) target.add_nonlocal_quantum_error(error2, ['cx'], [2, 0], [3, 1], False) self.assertEqual(remapped_model, target)
def get_noise(p1, p2, p4, pu3=0): '''Returns noise model for given error probabilities for 1, 2, and 4 qubit gates, as well as a separate probability for u3 gates ''' error_1 = depolarizing_error(p1, 1) error_2 = depolarizing_error(p2, 2) error_4 = depolarizing_error(p4, 4) error_u3 = depolarizing_error(pu3, 1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_1, ['x', 'h']) noise_model.add_all_qubit_quantum_error(error_2, ['cx']) noise_model.add_all_qubit_quantum_error(error_4, ['c3x']) noise_model.add_all_qubit_quantum_error(error_u3, ['u3']) return noise_model
def test_process_characterisation_complete_noise_model() -> None: my_noise_model = NoiseModel() readout_error_0 = 0.2 readout_error_1 = 0.3 my_noise_model.add_readout_error( [ [1 - readout_error_0, readout_error_0], [readout_error_0, 1 - readout_error_0], ], [0], ) my_noise_model.add_readout_error( [ [1 - readout_error_1, readout_error_1], [readout_error_1, 1 - readout_error_1], ], [1], ) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 1]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [0]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Z", 0.65)]), ["u2"], [0]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Y", 0.65)]), ["u1"], [0]) back = AerBackend(my_noise_model) char = cast(Dict[str, Any], back.characterisation) dev = Device( char.get("NodeErrors", {}), char.get("EdgeErrors", {}), char.get("Architecture", Architecture([])), ) assert char["GenericTwoQubitQErrors"][(0, 1)][0][1][0] == 0.0375 assert char["GenericTwoQubitQErrors"][(0, 1)][0][1][15] == 0.4375 assert char["GenericOneQubitQErrors"][0][0][1][0] == 0.125 assert char["GenericOneQubitQErrors"][0][0][1][3] == 0.625 assert char["GenericOneQubitQErrors"][0][1][1][0] == 0.35 assert char["GenericOneQubitQErrors"][0][1][1][1] == 0.65 assert char["GenericOneQubitQErrors"][0][2][1][0] == 0.35 assert char["GenericOneQubitQErrors"][0][2][1][1] == 0.65 assert dev.get_error(OpType.U3, dev.nodes[0]) == 0.375 assert dev.get_error(OpType.CX, (dev.nodes[0], dev.nodes[1])) == 0.5625 assert dev.get_error(OpType.CX, (dev.nodes[1], dev.nodes[0])) == 0.80859375 assert char["ReadoutErrors"][0] == [[0.8, 0.2], [0.2, 0.8]] assert char["ReadoutErrors"][1] == [[0.7, 0.3], [0.3, 0.7]]
def noise(prob_1=0.0, prob_2=0.0): # Depolarizing quantum errors error_1 = depolarizing_error(prob_1, 1) error_2 = depolarizing_error(prob_2, 2) # Add errors to noise model noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_1, ['u1', 'u2', 'u3']) noise_model.add_all_qubit_quantum_error(error_2, ['cx']) # Get basis gates from noise model basis_gates = noise_model.basis_gates return noise_model, basis_gates
def test_remap_quantum_errors(self): """Test remapping of quantum errors.""" model = NoiseModel() error1 = depolarizing_error(0.5, 1) error2 = depolarizing_error(0.5, 2) model.add_quantum_error(error1, ['u3'], [0], False) model.add_quantum_error(error2, ['cx'], [1, 2], False) with self.assertWarns(DeprecationWarning): remapped_model = remap_noise_model(model, [[0, 1], [1, 2], [2, 0]], warnings=False) target = NoiseModel() target.add_quantum_error(error1, ['u3'], [1], False) target.add_quantum_error(error2, ['cx'], [2, 0], False) self.assertEqual(remapped_model, target)
def test_truncate_nonlocal_noise(self): """Test qubit truncation with non-local noise.""" # Circuit that uses just 2-qubits circuit = QuantumCircuit(10, 1) circuit.x(5) circuit.measure(5, 0) # Add non-local 2-qubit depolarizing error # that acts on qubits [4, 6] when X applied to qubit 5 noise_model = NoiseModel() error = depolarizing_error(0.1, 2) noise_model.add_nonlocal_quantum_error(error, ['x'], [5], [4, 6]) qasm_sim = Aer.get_backend('qasm_simulator') backend_options = self.BACKEND_OPTS.copy() backend_options["truncate_verbose"] = True backend_options['optimize_ideal_threshold'] = 1 backend_options['optimize_noise_threshold'] = 1 result = execute(circuit, qasm_sim, shots=100, noise_model=noise_model, backend_options=backend_options).result() metadata = result.results[0].metadata self.assertTrue('truncate_qubits' in metadata, msg="truncate_qubits must work.") active_qubits = sorted(metadata['truncate_qubits'].get('active_qubits', [])) mapping = metadata['truncate_qubits'].get('mapping', []) active_remapped = sorted([i[1] for i in mapping if i[0] in active_qubits]) self.assertEqual(active_qubits, [4, 5, 6]) self.assertEqual(active_remapped, [0, 1, 2])
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 get_noise(noisy): """Returns a noise model when input is not False or None. A string will be interpreted as the name of a backend, and the noise model of this will be extracted. A float will be interpreted as an error probability for a depolarizing+measurement error model. Anything else (such as True) will give the depolarizing+measurement error model with default error probabilities.""" if noisy: if type(noisy) is str: # get noise information from a real device (via the IBM Q Experience) device = get_backend(noisy) noise_model = noise.device.basic_device_noise_model( device.properties() ) else: # make a simple noise model for a given noise strength if type(noisy) is float: p_meas = noisy p_gate1 = noisy else: # default values p_meas = 0.08 p_gate1 = 0.04 error_meas = pauli_error([('X',p_meas), ('I', 1 - p_meas)]) # bit flip error with prob p_meas error_gate1 = depolarizing_error(p_gate1, 1) # replaces qubit state with nonsense with prob p_gate1 error_gate2 = error_gate1.kron(error_gate1) # as above, but independently on two qubits noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") # add bit flip noise to measurement noise_model.add_all_qubit_quantum_error(error_gate1, ["u1", "u2", "u3"]) # add depolarising to single qubit gates noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) # add two qubit depolarising to two qubit gates else: noise_model = None return noise_model
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 fidelity_drop(self, qc, drawing=True, **kwargs): nqc = transpile(qc, optimization_level=0, basis_gates=['cx', 'u3']) # HACK efficient ways? if isinstance(self.one_error, (float, int)) and isinstance(self.two_error, (float, int)): fidelitis, std = self.fixed_fidelity(nqc) # FIXME more easy way self.pattern = 0 # FIXME more seeable elif isinstance(self.one_error, (float, int)) and isinstance(self.two_error, (np.ndarray, list)): fidelities, std = self._u3fix(nqc) self.pattern = 1 elif isinstance(self.two_error, (float, int)) and isinstance(self.one_error, (np.ndarray, list)): cxerror = depolarizing_error(self.two_error, 2) fidelities, std = self._cxfix(nqc) self.pattern = 2 else: fidelities, std = self._nofix(nqc) self.pattern = 3 if drawing: self._draw(fidelities, std, **kwargs) return fidelities
def test_raises_duplicate_qubits(self): """Test duplicate qubits raises exception""" model = NoiseModel() self.assertRaises(NoiseError, remap_noise_model, model, [[0, 1], [2, 1]], warnings=False) model = NoiseModel() error = depolarizing_error(0.5, 1) model.add_quantum_error(error, ['u3'], [2], False) self.assertRaises(NoiseError, remap_noise_model, model, [[3, 2]], warnings=False)
def amplified_qiskit_model(name, amplification=1, gate_time=0.001): provider = IBMQ.load_account() device = provider.get_backend(name) properties = device.properties() noise_model = NoiseModel() # depolarizing error cnot_depol_errs = [ x.parameters[0].value for x in properties.gates if x.gate == 'cx' ] ave_cnot_error = sum(cnot_depol_errs) / len( cnot_depol_errs) / amplification cnot_depol_error = depolarizing_error(ave_cnot_error, 2) single_depol_errs = [ x.parameters[0].value for x in properties.gates if x.gate != 'cx' ] ave_single_error = sum(single_depol_errs) / len( single_depol_errs) / amplification single_depol_error = depolarizing_error(ave_single_error, 1) # amp damp t1s = [x[0].value for x in properties.qubits] ave_t1 = sum(t1s) / len(t1s) t1_ratio = gate_time / (ave_t1 * amplification) amp_gamma = 1 - np.exp(-t1_ratio) amp_error = amplitude_damping_error(amp_gamma) # phase damp t2s = [x[1].value for x in properties.qubits] ave_t2 = sum(t2s) / len(t2s) t2_ratio = gate_time / (ave_t2 * amplification) phase_gamma = 1 - np.exp(-t2_ratio) phase_error = phase_damping_error(phase_gamma) thermal_error = amp_error.compose(phase_error) noise_model.add_all_qubit_quantum_error( single_depol_error.compose(thermal_error), ['u1', 'u2', 'u3']) noise_model.add_all_qubit_quantum_error( cnot_depol_error.compose(thermal_error.tensor(thermal_error)), ['cx']) return noise_model
def get_noise(p_meas, p_gate): error_meas = pauli_error([('X', p_meas), ('I', 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") noise_model.add_all_qubit_quantum_error(error_gate1, ["x"]) noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) return noise_model
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 get_noise(p_meas,p_gate): error_meas = pauli_error([('X',p_meas), ('I', 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") # measurement error is applied to measurements noise_model.add_all_qubit_quantum_error(error_gate1, ["x"]) # single qubit gate error is applied to x gates noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) # two qubit gate error is applied to cx gates return noise_model
def noise_model_depol(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( depolarizing_error(gate_error, num_qubits), gate) return noise
def get_noise(p_meas, p_gate): """Define a noise model.""" error_meas = pauli_error([("X", p_meas), ("I", 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") noise_model.add_all_qubit_quantum_error(error_gate1, ["u1", "u2", "u3"]) noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) return noise_model
def __get_counts(self, params: List[float or int], shots: int = 1000) -> dict: """ Here we run the circuit according to the given parameters for each gate and return the counts for each state. :param params: List of the parameters of the RY and RX gates of the circuit. :param shots: Total number of shots the circuit must execute """ # Error probabilities prob_1 = 0.001 # 1-qubit gate prob_2 = 0.01 # 2-qubit gate # Depolarizing quantum errors error_1 = depolarizing_error(prob_1, 1) error_2 = depolarizing_error(prob_2, 2) # Add errors to noise model noise_model = noise.NoiseModel() noise_model.add_all_qubit_quantum_error(error_1, ['u1', 'u2']) noise_model.add_all_qubit_quantum_error(error_2, ['cx']) # Get basis gates from noise model basis_gates = noise_model.basis_gates # Make a circuit circ = QuantumCircuit(2, 2) # Set gates and measurement circ.ry(params[0], 0) circ.rx(params[1], 1) circ.cx(0, 1) circ.measure([0, 1], [0, 1]) # Perform a noisy simulation and get the counts # noinspection PyTypeChecker result = execute(circ, Aer.get_backend('qasm_simulator'), basis_gates=basis_gates, noise_model=noise_model, shots=shots).result() counts = result.get_counts(0) return counts
def test_process_characterisation_incomplete_noise_model() -> None: my_noise_model = NoiseModel() my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 1]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [1]) my_noise_model.add_quantum_error(depolarizing_error(0.1, 1), ["u3"], [3]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Z", 0.65)]), ["u2"], [0]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Y", 0.65)]), ["u1"], [2]) back = AerBackend(my_noise_model) char = cast(Dict[str, Any], back.characterisation) c = Circuit(4).CX(0, 1).H(2).CX(2, 1).H(3).CX(0, 3).H(1).X(0).measure_all() back.compile_circuit(c) assert back.valid_circuit(c) dev = Device( char.get("NodeErrors", {}), char.get("EdgeErrors", {}), char.get("Architecture", Architecture([])), ) nodes = dev.nodes assert set(dev.architecture.coupling) == set([ (nodes[0], nodes[1]), (nodes[0], nodes[2]), (nodes[0], nodes[3]), (nodes[1], nodes[2]), (nodes[1], nodes[3]), (nodes[2], nodes[0]), (nodes[2], nodes[1]), (nodes[2], nodes[3]), (nodes[3], nodes[0]), (nodes[3], nodes[1]), (nodes[3], nodes[2]), ])
def _nofix(self, qc): nst = 2**len(self.mes_qbt) bins = [format(i, '0%db' % len(self.mes_qbt)) for i in range(nst)] # ideal result of this circuit ideal = execute(qc, backend=QASM, shots=self.shots * 10) idealcounts = ideal.result().get_counts() idealst = np.array( [idealcounts.get(i, 0) / (self.shots * 10) for i in bins]) # start simulations if len(self.one_error) != len(self.two_error): raise ValueError('length of array of one error \ and two error must be the same.') mean_fid = [] std_fid = [] for one_er, two_er in tqdm(zip(self.one_error, self.two_error)): mid = [] # HACK: might be efficient in top layer noise_model = NoiseModel() u3error = depolarizing_error(one_er, 1) cxerror = depolarizing_error(two_er, 2) noise_model.add_all_qubit_quantum_error(u3error, ['u3']) noise_model.add_all_qubit_quantum_error(cxerror, ['cx']) for t in range(self.extime): job = execute(qc, backend=QASM, noise_model=noise_model, shots=self.shots) counts = job.result().get_counts() stvec = [counts.get(i, 0) / self.shots for i in bins] stf = state_fidelity(idealst, stvec) mid.append(stf) mean_fid.append(np.mean(mid)) std_fid.append(np.std(mid)) return mean_fid, std_fid
def test_circuit_compilation_complete_noise_model() -> None: my_noise_model = NoiseModel() my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 1]) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 2]) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 3]) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [1, 2]) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [1, 3]) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [2, 3]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [0]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [1]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [2]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [3]) back = AerBackend(my_noise_model) c = Circuit(4).CX(0, 1).H(2).CX(2, 1).H(3).CX(0, 3).H(1).X(0).measure_all() back.compile_circuit(c) assert back.valid_circuit(c)
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_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)
# + ex1_mean = [] ex1_std = [] ex2_mean = [] ex2_std = [] ex3_mean = [] ex3_std = [] ex4_mean = [] ex4_std = [] extime = 10 u3_error = depolarizing_error(0.001, 1) qw_step = range(1, 11) gate_error = np.arange(0, 0.03, 0.001) # errors, steps= np.meshgrid(gate_error, qw_step) bins = [format(i, '02b') for i in range(2**2)] step = 5 opt_qc = four_node(True, step) job = execute(opt_qc, backend=qasm_sim, shots=100000) count = job.result().get_counts(opt_qc) ideal_prob = [count.get(i, 0)/100000 for i in bins] for cxerr in tqdm(gate_error): # noise model error_model = NoiseModel() cx_error = depolarizing_error(cxerr, 2) error_model.add_all_qubit_quantum_error(u3_error, ['u3', 'u2'])
# 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. # # It is added as `noise_model.add_all_qubit_quantum_error(error, instructions)`: # In[9]: # Create an empty noise model noise_model = NoiseModel() # Add depolarizing error to all single qubit u1, u2, u3 gates error = depolarizing_error(0.05, 1) noise_model.add_all_qubit_quantum_error(error, ['u1', 'u2', 'u3']) # Print noise model info print(noise_model) # ### Specific qubit quantum error # # This applies the error to any occurrence of an instruction acting on a specified list of qubits. Note that the order of the qubit matters: For a 2-qubit gate an error applied to qubits [0, 1] is different to one applied to qubits [1, 0] for example. # # It is added as `noise_model.add_quantum_error(error, instructions, qubits)`: # In[10]: # Create an empty noise model noise_model = NoiseModel()
def get_noise(p): error_1 = depolarizing_error(p, 1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_1, ['x', 'u3']) return noise_model