def test_t1(self): """ Run the simulator with thermal relaxatoin noise. Then verify that the calculated T1 matches the t1 parameter. """ # 25 numbers ranging from 1 to 200, linearly spaced num_of_gates = (np.linspace(1, 200, 15)).astype(int) gate_time = 0.11 qubits = [0] circs, xdata = t1_circuits(num_of_gates, gate_time, qubits) expected_t1 = 10 error = thermal_relaxation_error(expected_t1, 2 * expected_t1, gate_time) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') # TODO: Include SPAM errors backend = qiskit.Aer.get_backend('qasm_simulator') shots = 100 backend_result = qiskit.execute(circs, backend, shots=shots, backend_options={ 'max_parallel_experiments': 0 }, noise_model=noise_model).result() initial_t1 = expected_t1 initial_a = 1 initial_c = 0 T1Fitter(backend_result, xdata, qubits, fit_p0=[initial_a, initial_t1, initial_c], fit_bounds=([0, 0, -1], [2, expected_t1 * 1.2, 1]))
def test_t2(self): """ Run the simulator with thermal relaxation noise. Then verify that the calculated T2 matches the t2 parameter. """ num_of_gates = (np.linspace(1, 30, 10)).astype(int) gate_time = 0.11 qubits = [0] n_echos = 5 alt_phase_echo = True circs, xdata = t2_circuits(num_of_gates, gate_time, qubits, n_echos, alt_phase_echo) expected_t2 = 20 error = thermal_relaxation_error(np.inf, expected_t2, gate_time, 0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') # TODO: Include SPAM errors backend = qiskit.Aer.get_backend('qasm_simulator') shots = 100 backend_result = qiskit.execute(circs, backend, shots=shots, backend_options={ 'max_parallel_experiments': 0 }, noise_model=noise_model).result() initial_t2 = expected_t2 initial_a = 1 initial_c = 0.5 * (-1) T2Fitter(backend_result, xdata, qubits, fit_p0=[initial_a, initial_t2, initial_c], fit_bounds=([0, 0, -1], [2, expected_t2 * 1.2, 1]))
def t2_circuit_execution( ) -> Tuple[qiskit.result.Result, np.array, List[int], float]: """ Create T2 circuits and simulate them. Returns: * Backend result. * xdata. * Qubits for the T2 measurement. * T2 that was used in the circuits creation. """ num_of_gates = (np.linspace(1, 30, 10)).astype(int) gate_time = 0.11 qubits = [0] n_echos = 5 alt_phase_echo = True circs, xdata = t2_circuits(num_of_gates, gate_time, qubits, n_echos, alt_phase_echo) t2_value = 20 error = thermal_relaxation_error(np.inf, t2_value, gate_time, 0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') # TODO: Include SPAM errors backend = qiskit.Aer.get_backend('qasm_simulator') shots = 100 backend_result = qiskit.execute(circs, backend, shots=shots, seed_simulator=SEED, backend_options={ 'max_parallel_experiments': 0 }, noise_model=noise_model, optimization_level=0).result() return backend_result, xdata, qubits, t2_value
def test_backend_method_nonclifford_circuit_and_reset_noise(self): """Test statevector method is used for Clifford circuit""" # Test noise model noise_circs = [[{ "name": "reset", "qubits": [0] }], [{ "name": "id", "qubits": [0] }]] noise_probs = [0.5, 0.5] error = QuantumError(zip(noise_circs, noise_probs)) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error( error, ['id', 'x', 'y', 'z', 'h', 's', 'sdg']) # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) def get_result(): return self.SIMULATOR.run(qobj, backend_options=self.BACKEND_OPTS, noise_model=noise_model).result() # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertRaises(AerError, get_result) else: result = get_result() self.is_completed(result) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method)
def rb_circuit_execution_2(rb_opts: dict, shots: int): """ Create rb circuits with T1 and T2 errors and simulate them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the circuits simulations list: the xdata of the rb circuit """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_circs, xdata = rb.randomized_benchmarking_seq(**rb_opts) noise_model = NoiseModel() # Add T1/T2 noise to the simulation t_1 = 100. t_2 = 80. gate1q = 0.1 gate2q = 0.5 noise_model.add_all_qubit_quantum_error(thermal_relaxation_error(t_1, t_2, gate1q), 'u2') noise_model.add_all_qubit_quantum_error(thermal_relaxation_error(t_1, t_2, 2 * gate1q), 'u3') noise_model.add_all_qubit_quantum_error( thermal_relaxation_error(t_1, t_2, gate2q).tensor( thermal_relaxation_error(t_1, t_2, gate2q)), 'cx') results = [] for circuit in rb_circs: results.append(qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) return results, xdata
def build_noise_model(noise_rates, multiplier): noise_model = NoiseModel( basis_gates=['cx', 'id', 'reset', 'rz', 'sx', 'x']) gate_errors = {x[0]: x[1] * multiplier for x in noise_rates.items()} for gate in noise_rates: #Depolarizing error on CX if gate == 'cx': depol_err_cx = depolarizing_error(noise_rates[gate] * multiplier, 2) noise_model.add_all_qubit_quantum_error(depol_err_cx, ['cx']) #Depolarizing error on single qubit gates else: depol_err = depolarizing_error(noise_rates[gate] * multiplier, 1, standard_gates=False) noise_model.add_all_qubit_quantum_error(depol_err, [gate]) #Save error rates with open("gate_errors_{}.json".format(multiplier), "w") as f: json.dump(gate_errors, f) return noise_model
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.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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) 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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) 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_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_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 compiled 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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) # 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_backend_method_clifford_circuits_and_pauli_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=True) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, **self.BACKEND_OPTS).result() success = getattr(result, 'success', False) self.assertTrue(success) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method != 'automatic': self.compare_result_metadata(result, circuits, 'method', method) else: self.compare_result_metadata(result, circuits, 'method', 'stabilizer')
def test_transpiling(self): qr = QuantumRegister(3, 'qr') circuit = QuantumCircuit(qr) circuit.x(qr[0]) circuit.y(qr[1]) circuit.z(qr[2]) error_x = pauli_error([('Y', 0.25), ('I', 0.75)]) error_y = pauli_error([('X', 0.35), ('Z', 0.65)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_x, 'x') noise_model.add_all_qubit_quantum_error(error_y, 'u1') target_circuit = QuantumCircuit(qr) target_circuit.x(qr[0]) target_circuit.append(error_x.to_instruction(), [qr[0]]) target_circuit.u3(pi, pi / 2, pi / 2, qr[1]) target_circuit.u1(pi, qr[2]) target_circuit.append(error_y.to_instruction(), [qr[2]]) result_circuit = insert_noise(circuit, noise_model, transpile=True) self.assertEqual(target_circuit, result_circuit)
def thermalRelaxationChannel(backend, T1s, T2s, graph, gates): '''Method that returns the thermal relaxation error quantum channel.''' # Instruction times (in nanoseconds) time_u1 = 10 # virtual gate time_u2 = getSQGateExecutionTime('x', backend, gates) # Average of all u2 times time_u3 = getSQGateExecutionTime('x', backend, gates) # Average of all u3 times time_cx = getTQGateExecutionTime('cx', backend, graph) # Average of all cx times time_reset = 1000 # 1 microsecond time_measure = 1000 # 1 microsecond # QuantumError objects errors_reset = [thermal_relaxation_error(t1, t2, time_reset) for t1, t2 in zip(T1s, T2s)] errors_measure = [thermal_relaxation_error(t1, t2, time_measure) for t1, t2 in zip(T1s, T2s)] errors_u1 = [thermal_relaxation_error(t1, t2, time_u1) for t1, t2 in zip(T1s, T2s)] errors_u2 = [thermal_relaxation_error(t1, t2, time_u2) for t1, t2 in zip(T1s, T2s)] errors_u3 = [thermal_relaxation_error(t1, t2, time_u3) for t1, t2 in zip(T1s, T2s)] errors_cx = [[thermal_relaxation_error(t1a, t2a, time_cx).expand( thermal_relaxation_error(t1b, t2b, time_cx)) for t1a, t2a in zip(T1s, T2s)] for t1b, t2b in zip(T1s, T2s)] # Add errors to noise model noise_thermal = NoiseModel() for j in range(len(T1s)): noise_thermal.add_quantum_error(errors_reset[j], "reset", [j]) noise_thermal.add_quantum_error(errors_measure[j], "measure", [j]) noise_thermal.add_quantum_error(errors_u1[j], "u1", [j]) noise_thermal.add_quantum_error(errors_u2[j], "u2", [j]) noise_thermal.add_quantum_error(errors_u3[j], "u3", [j]) for k in range(len(T1s)): noise_thermal.add_quantum_error(errors_cx[j][k], "cx", [j, k]) return noise_thermal
def t1_circuit_execution( ) -> Tuple[qiskit.result.Result, np.array, List[int], float]: """ Create T1 circuits and simulate them. Returns: * Backend result. * xdata. * Qubits for the T1 measurement. * T1 that was used in the circuits creation. """ # 15 numbers ranging from 1 to 200, linearly spaced num_of_gates = (np.linspace(1, 200, 15)).astype(int) gate_time = 0.11 qubits = [0] circs, xdata = t1_circuits(num_of_gates, gate_time, qubits) t1_value = 10 error = thermal_relaxation_error(t1_value, 2 * t1_value, gate_time) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') # TODO: Include SPAM errors backend = qiskit.Aer.get_backend('qasm_simulator') shots = 100 backend_result = qiskit.execute(circs, backend, shots=shots, seed_simulator=SEED, backend_options={ 'max_parallel_experiments': 0 }, noise_model=noise_model, optimization_level=0).result() return backend_result, xdata, qubits, t1_value
def thermal_noise(n_qubits,mu1,mu2,sigma): num = n_qubits # T1 and T2 values for qubits 0-num T1s = np.random.normal(mu1, sigma, num) # Sampled from normal distribution mean mu1 microsec T2s = np.random.normal(mu2, sigma, num) # Sampled from normal distribution mean mu2 microsec # Truncate random T2s <= T1s T2s = np.array([min(T2s[j], 2 * T1s[j]) for j in range(num)]) # Instruction times (in nanoseconds) time_u1 = 0 # virtual gate time_u2 = 50 # (single X90 pulse) time_u3 = 100 # (two X90 pulses) time_cx = 300 # QuantumError objects errors_u1 = [thermal_relaxation_error(t1, t2, time_u1) for t1, t2 in zip(T1s, T2s)] errors_u2 = [thermal_relaxation_error(t1, t2, time_u2) for t1, t2 in zip(T1s, T2s)] errors_u3 = [thermal_relaxation_error(t1, t2, time_u3) for t1, t2 in zip(T1s, T2s)] errors_cx = [[thermal_relaxation_error(t1a, t2a, time_cx).expand( thermal_relaxation_error(t1b, t2b, time_cx)) for t1a, t2a in zip(T1s, T2s)] for t1b, t2b in zip(T1s, T2s)] # Add errors to noise model noise_thermal = NoiseModel() for j in range(num): noise_thermal.add_quantum_error(errors_u1[j], "u1", [j]) noise_thermal.add_quantum_error(errors_u2[j], "u2", [j]) noise_thermal.add_quantum_error(errors_u3[j], "u3", [j]) for k in range(num): noise_thermal.add_quantum_error(errors_cx[j][k], "cx", [j, k]) return noise_thermal
def create_quantum_computer_simulation(couplingmap, depolarizingnoise=False, depolarizingnoiseparameter=0, bitfliperror=False, bitfliperrorparameter=0, measerror=False, measerrorparameter=0): """ Returns a dictionary, where the key is what type of simulation ("noisy_qasm", "noiseless_qasm", "real"), and the value are the objects required for that particular simulation """ sims = ["noisy_qasm", "noiseless_qasm"] dicto = dict() for sim in sims: if sim == "noiseless_qasm": backend = Aer.get_backend('qasm_simulator') dicto[sim] = backend elif sim == "noisy_qasm": backend = Aer.get_backend('qasm_simulator') coupling_map = CouplingMap(couplingmap) noise_model = NoiseModel() if depolarizingnoise == True: depolarizingerror = depolarizing_error( depolarizingnoiseparameter, 1) noise_model.add_all_qubit_quantum_error( depolarizingerror, ['u1', 'u2', 'u3']) if bitfliperror == True: error_gate1 = pauli_error([('X', bitfliperrorparameter), ('I', 1 - bitfliperrorparameter)]) noise_model.add_all_qubit_quantum_error( error_gate1, ["u1", "u2", "u3"]) error_gate2 = error_gate1.tensor(error_gate1) noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) if measerror == True: error_meas = pauli_error([('X', measerrorparameter), ('I', 1 - measerrorparameter)]) noise_model.add_all_qubit_quantum_error(error_meas, "measure") dicto[sim] = (backend, coupling_map, noise_model) return dicto
def test_local_quantum_errors(self): qr = QuantumRegister(3, 'qr') circuit = QuantumCircuit(qr) circuit.x(qr[0]) circuit.x(qr[1]) circuit.y(qr[2]) error_x = pauli_error([('Y', 0.25), ('I', 0.75)]) error_y = pauli_error([('X', 0.35), ('Z', 0.65)]) noise_model = NoiseModel() noise_model.add_quantum_error(error_x, 'x', [0]) noise_model.add_quantum_error(error_y, 'y', [2]) target_circuit = QuantumCircuit(qr) target_circuit.x(qr[0]) target_circuit.append(error_x.to_instruction(), [qr[0]]) target_circuit.x(qr[1]) target_circuit.y(qr[2]) target_circuit.append(error_y.to_instruction(), [qr[2]]) result_circuit = insert_noise(circuit, noise_model) self.assertEqual(target_circuit, result_circuit)
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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) 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_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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) 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_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 = 1000 # 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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) 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_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} qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) result = backend.run(qobj, noise_model=noise_model).result() self.is_completed(result) self.compare_counts(result, [circuit], [target], delta=0)
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) sampling = (self.BACKEND_OPTS.get("method", "automatic").startswith("density_matrix")) self.compare_result_metadata(result, circuits, "measure_sampling", sampling)
def test_transpiling(self): qr = QuantumRegister(3, 'qr') circuit = QuantumCircuit(qr) circuit.x(qr[0]) circuit.y(qr[1]) circuit.z(qr[2]) error_x = pauli_error([('Y', 0.25), ('I', 0.75)]) error_y = pauli_error([('X', 0.35), ('Z', 0.65)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_x, 'x') noise_model.add_all_qubit_quantum_error(error_y, 'y') target_circuit = QuantumCircuit(qr) target_circuit.x(qr[0]) target_circuit.append(error_x.to_instruction(), [qr[0]]) target_circuit.y(qr[1]) target_circuit.append(error_y.to_instruction(), [qr[1]]) target_circuit.z(qr[2]) target_basis = ['kraus'] + noise_model.basis_gates target_circuit = transpile(target_circuit, basis_gates=target_basis) result_circuit = insert_noise(circuit, noise_model, transpile=True) self.assertEqual(SuperOp(target_circuit), SuperOp(result_circuit))
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 rb_circuit_execution(rb_opts: dict, shots: int): """ Create rb circuits with depolarizing error and simulates them Args: rb_opts: the options for the rb circuits shots: number of shots for each circuit simulation Returns: list: list of Results of the circuits simulations list: the xdata of the rb circuit """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_circs, xdata = rb.randomized_benchmarking_seq(**rb_opts) noise_model = NoiseModel() p1q = 0.002 p2q = 0.01 noise_model.add_all_qubit_quantum_error(depolarizing_error(p1q, 1), 'u2') noise_model.add_all_qubit_quantum_error(depolarizing_error(2 * p1q, 1), 'u3') noise_model.add_all_qubit_quantum_error(depolarizing_error(p2q, 2), 'cx') results = [] for circuit in rb_circs: results.append( qiskit.execute(circuit, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) return results, xdata
def create_pta_channel(T_1, T_2, t_step=10e-9): """ Creates a noise model that does both phase and amplitude damping but in the Pauli Twirling Approximation discussed the following reference https://arxiv.org/pdf/1305.2021.pdf Args: T_1 (float) : Relaxation time (seconds) T_2 (float) : dephasing time (seconds) t_step (float) : Discretized time step over which the relaxation occurs over (seconds) Returns: qiskit.providers.aer.noise.NoiseModel """ if T_1 == T_2: t_phi = 2*T_1 elif 2*T_1 == T_2: raise RuntimeError(" T_2 == 2*T_1 only in a pure amplitude damping case ") else: t_phi = T_2 - 2*T_1 p_x = 0.25*(1- pow(np.e, - t_step/T_1)) p_y = 0.25*(1- pow(np.e, - t_step/T_1)) exp_1 = pow(np.e, -t_step/(2*T_1)) exp_2 = pow(np.e, -t_step/t_phi) p_z = (0.5 - p_x - 0.5*exp_1*exp_2) p_i = 1 - p_x - p_y - p_z errors = [('X', p_x), ('Y', p_y), ('Z', p_z), ('I', p_i)] pta_error = pauli_error(errors) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(pta_error, ['id', 'u3']) gate_error = pta_error.tensor(pta_error) noise_model.add_all_qubit_quantum_error(gate_error, ['cx']) return noise_model
def meas_calibration_circ_execution(shots: int, seed: int): """ create measurement calibration circuits and simulate them with noise Args: shots (int): number of shots per simulation seed (int): the seed to use in the simulations Returns: list: list of Results of the measurement calibration simulations list: list of all the possible states with this amount of qubits dict: dictionary of results counts of GHZ circuit simulation with measurement errors """ # define the circuits meas_calibs, state_labels, ghz = meas_calib_circ_creation() # define noise prob = 0.2 error_meas = pauli_error([('X', prob), ('I', 1 - prob)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") # run the circuits multiple times backend = qiskit.Aer.get_backend('qasm_simulator') cal_results = qiskit.execute(meas_calibs, backend=backend, shots=shots, noise_model=noise_model, seed_simulator=seed).result() ghz_results = qiskit.execute(ghz, backend=backend, shots=shots, noise_model=noise_model, seed_simulator=seed).result().get_counts() return cal_results, state_labels, ghz_results
def scale_noise(pq: QuantumCircuit, param: float) -> QuantumCircuit: """Scales the noise in a quantum circuit of the factor `param`. Args: pq: Quantum circuit. noise: Noise constant going into `depolarizing_error`. shots: Number of shots to run the circuit on the back-end. Returns: pq: quantum circuit as a :class:`qiskit.QuantumCircuit` object. """ global CURRENT_NOISE noise = param * NATIVE_NOISE assert noise <= 1.0, ( "Noise scaled to {} is out of bounds (<=1.0) for depolarizing " "channel.".format(noise)) noise_model = NoiseModel() # we assume a depolarizing error for each gate of the standard IBM basis # set (u1, u2, u3) noise_model.add_all_qubit_quantum_error(depolarizing_error(noise, 1), ["u1", "u2", "u3"]) CURRENT_NOISE = noise_model return pq
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 } qobj = compile([circuit], backend, shots=shots, basis_gates=noise_model.basis_gates) result = backend.run(qobj, noise_model=noise_model).result() self.is_completed(result) self.compare_counts(result, [circuit], [target], delta=0.05 * shots)
def noise_model_bitphase(error, measure=True, thermal=True): """ Creates error for bitphase flip :param error: Probability of happening a bitphase flip :param measure: True or False, whether we include readout errors with probability 10 * error :param thermal: True or False, whether we include thermal relaxation, see noise_model_thermal :return: noise model for the error """ noise_model = NoiseModel() # basis_gates=['id', 'u2', 'u3', 'cx']) cnot_error = 2 * error bit_flip = pauli_error([('X', error), ('I', 1 - error)]) phase_flip = pauli_error([('Z', error), ('I', 1 - error)]) bitphase_flip = bit_flip.compose(phase_flip) cnot_flip = pauli_error([('X', cnot_error), ('I', 1 - cnot_error)]) cnot_phase = pauli_error([('Z', cnot_error), ('I', 1 - cnot_error)]) cnot_error = cnot_flip.compose(cnot_phase) cnot_error = cnot_error.tensor(cnot_error) noise_model.add_all_qubit_quantum_error(cnot_error, ['cx'], warnings=False) noise_model.add_all_qubit_quantum_error(bitphase_flip, ["u1", "u2", "u3"]) if measure: measure_error = 10 * error measure_error = array([[1 - measure_error, measure_error], [measure_error, 1 - measure_error]]) noise_model.add_all_qubit_readout_error(measure_error) if thermal: thermal = thermal_relaxation_error(1.5, 1.2, error) cthermal = thermal_relaxation_error(1.5, 1.2, 2 * error) cthermal = cthermal.tensor(cthermal) noise_model.add_all_qubit_quantum_error(cthermal, ['cx'], warnings=False) noise_model.add_all_qubit_quantum_error(thermal, ["u1", "u2", "u3"], warnings=False) return noise_model