def create_high_noise_model(): """ create high noise model with depolarizing error, thermal error and readout error Returns: NoiseModel: noise model """ noise_model = NoiseModel() p1q = 0.004 p2q = 0.05 depol_sx = depolarizing_error(p1q, 1) depol_x = depolarizing_error(p1q, 1) depol_cx = depolarizing_error(p2q, 2) # Add T1/T2 noise to the simulation t_1 = 110e2 t_2 = 120e2 gate1q = 50 gate2q = 100 termal_sx = thermal_relaxation_error(t_1, t_2, gate1q) termal_x = thermal_relaxation_error(t_1, t_2, gate1q) termal_cx = thermal_relaxation_error(t_1, t_2, gate2q).tensor( thermal_relaxation_error(t_1, t_2, gate2q) ) noise_model.add_all_qubit_quantum_error(depol_sx.compose(termal_sx), "sx") noise_model.add_all_qubit_quantum_error(depol_x.compose(termal_x), "x") noise_model.add_all_qubit_quantum_error(depol_cx.compose(termal_cx), "cx") read_err = readout_error.ReadoutError([[0.98, 0.02], [0.04, 0.96]]) noise_model.add_all_qubit_readout_error(read_err) return noise_model
def test_thermal_relaxation_error_raises_invalid_t1(self): """Test raises error for invalid t1 parameters""" # T1 == 0 self.assertRaises(NoiseError, lambda: thermal_relaxation_error(0, 0, 0)) # T1 < 0 self.assertRaises(NoiseError, lambda: thermal_relaxation_error(-0.1, 0.1, 0))
def test_thermal_relaxation_error_raises_invalid_t2(self): """Test raises error for invalid t2 parameters""" # T2 == 0 self.assertRaises(NoiseError, lambda: thermal_relaxation_error(1, 0, 0)) # T2 < 0 self.assertRaises(NoiseError, lambda: thermal_relaxation_error(1, -1, 0))
def test_thermal_relaxation_error_t1_t2_inf_ideal(self): """Test t1 = t2 = inf returns identity channel""" error = thermal_relaxation_error(np.inf, np.inf, 0) circ, p = error.error_term(0) self.assertEqual(p, 1, msg="ideal probability") self.assertEqual(circ[0], {"name": "id", "qubits": [0]}, msg="ideal circuit")
def test_thermal_relaxation_error_zero_time_ideal(self): """Test gate_time = 0 returns identity channel""" error = thermal_relaxation_error(2, 3, 0) circ, p = error.error_term(0) self.assertEqual(p, 1, msg="ideal probability") self.assertEqual(circ[0], {"name": "id", "qubits": [0]}, msg="ideal circuit")
def test_thermal_relaxation_error_gate(self): """Test qobj instructions return for t2 < t1""" t1, t2, time, p1 = (2, 1, 1, 0.3) error = thermal_relaxation_error(t1, t2, time, p1) targets = [[{'name': 'id', 'qubits': [0]}], [{'name': 'z', 'qubits': [0]}], [{'name': 'reset', 'qubits': [0]}], [{'name': 'reset', 'qubits': [0]}, {'name': 'x', 'qubits': [0]}]] p_reset0 = (1 - p1) * (1 - np.exp(-1 / t1)) p_reset1 = p1 * (1 - np.exp(-1 / t1)) p_z = 0.5 * np.exp(-1 / t1) * (1 - np.exp(-(1 / t2 - 1 / t1) * time)) p_id = 1 - p_z - p_reset0 - p_reset1 for j in range(4): circ, p = error.error_term(j) self.remove_if_found(circ, targets) name = circ[0]['name'] if circ[0]['name'] == 'id': self.assertAlmostEqual(p, p_id, msg="identity probability") elif name == 'z': self.assertAlmostEqual(p, p_z, msg="Z error probability") elif len(circ) == 1: self.assertAlmostEqual(p, p_reset0, msg="reset-0 probability") else: self.assertAlmostEqual(p, p_reset1, msg="reset-1 probability") self.assertEqual(targets, [], msg="relaxation circuits")
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_noise_model_from_invalid_t2_backend(self): """Test if issue user warning when creating a noise model from invalid t2 backend""" from qiskit.providers.models.backendproperties import BackendProperties, Gate, Nduv import datetime t1_ns, invalid_t2_ns = 75_1000, 200_1000 u3_time_ns = 320 frequency = 4919.96800692 class InvalidT2Fake1Q(mock.FakeBackend): def __init__(self): mock_time = datetime.datetime.now() dt = 1.3333 configuration = BackendProperties( backend_name="invalid_t2", backend_version="0.0.0", num_qubits=1, basis_gates=["u3"], qubits=[ [ Nduv(date=mock_time, name="T1", unit="µs", value=t1_ns/1000), Nduv(date=mock_time, name="T2", unit="µs", value=invalid_t2_ns/1000), Nduv(date=mock_time, name="frequency", unit="MHz", value=frequency), ], ], gates=[ Gate( gate="u3", name="u3_0", qubits=[0], parameters=[ Nduv(date=mock_time, name="gate_error", unit="", value=0.001), Nduv(date=mock_time, name="gate_length", unit="ns", value=u3_time_ns), ], ), ], last_update_date=mock_time, general=[], ) super().__init__(configuration) def defaults(self): """defaults == configuration""" return self._configuration def properties(self): """properties == configuration""" return self._configuration backend = InvalidT2Fake1Q() with self.assertWarns(UserWarning): noise_model = NoiseModel.from_backend(backend, gate_error=False) expected = thermal_relaxation_error( t1=t1_ns, t2=2*t1_ns, time=u3_time_ns, excited_state_population=_excited_population(frequency, temperature=0) ) self.assertEqual(expected, noise_model._local_quantum_errors["u3"][(0, )])
def test_thermal_relaxation_error_kraus(self): """Test non-kraus instructions return for t2 < t1""" t1, t2, time, p1 = (1, 2, 1, 0.3) error = thermal_relaxation_error(t1, t2, time, p1) circ, p = error.error_term(0) self.assertEqual(p, 1) self.assertEqual(circ[0]['name'], 'kraus') self.assertEqual(circ[0]['qubits'], [0])
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 test_thermal_relaxation_error_t1_equal_t2_1state(self): """Test qobj instructions return for t1=t2""" actual = thermal_relaxation_error(1, 1, 1, 1) expected = QuantumError([ (IGate(), np.exp(-1)), ([(Reset(), [0]), (XGate(), [0])], 1 - np.exp(-1)), ]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def test_thermal_relaxation_error_t1_equal_t2_1state(self): """Test qobj instructions return for t1=t2""" error = thermal_relaxation_error(1, 1, 1, 1) targets = [[{'name': 'id', 'qubits': [0]}], [{'name': 'reset', 'qubits': [0]}, {'name': 'x', 'qubits': [0]}]] probs = [np.exp(-1), 1 - np.exp(-1)] for j in range(2): circ, p = error.error_term(j) self.remove_if_found(circ, targets) if circ[0]['name'] == 'id': self.assertAlmostEqual(p, probs[0], msg="identity probability") else: self.assertAlmostEqual(p, probs[1], msg="reset probability") self.assertEqual(targets, [], msg="relaxation circuits")
def t2_star_noise_model(gate_time=0.1, t=[70.5, 85.0, 80.0, 90.5, 77.5]): """ Return a NoiseModel object for T2*. Parameters - gate_time: gate time (in microseconds) for a single-qubit gate - t: simulated times (in microseconds) for a set of five qubits """ t2_star_noise_model = NoiseModel() error = [0 for i in range(5)] for i in range(5): error[i] = thermal_relaxation_error(np.inf, t[i], gate_time, 0.5) t2_star_noise_model.add_quantum_error(error[i], 'id', [i]) return t2_star_noise_model
def test_thermal_relaxation_error_gate(self): """Test qobj instructions return for t2 < t1""" t1, t2, time, p1 = (2, 1, 1, 0.3) actual = thermal_relaxation_error(t1, t2, time, p1) p_z = 0.5 * np.exp(-1 / t1) * (1 - np.exp(-(1 / t2 - 1 / t1) * time)) p_reset0 = (1 - p1) * (1 - np.exp(-1 / t1)) p_reset1 = p1 * (1 - np.exp(-1 / t1)) expected = QuantumError([ (IGate(), 1 - p_z - p_reset0 - p_reset1), (ZGate(), p_z), (Reset(), p_reset0), ([(Reset(), [0]), (XGate(), [0])], p_reset1), ]) for i in range(actual.size): circ, prob = actual.error_term(i) expected_circ, expected_prob = expected.error_term(i) self.assertEqual(circ, expected_circ, msg=f"Incorrect {i}-th circuit") self.assertAlmostEqual(prob, expected_prob, msg=f"Incorrect {i}-th probability")
def t2star_circuit_execution( ) -> Tuple[qiskit.result.Result, np.array, List[int], float, float]: """ Create T2* circuits and simulate them. Returns: * Backend result. * xdata. * Qubits for the T2* measurement. * T2* that was used in the circuits creation. * Frequency. """ # Setting parameters num_of_gates = np.append((np.linspace(10, 150, 10)).astype(int), (np.linspace(160, 450, 5)).astype(int)) gate_time = 0.1 qubits = [0] t2_value = 10 error = thermal_relaxation_error(np.inf, t2_value, gate_time, 0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') backend = qiskit.Aer.get_backend('qasm_simulator') shots = 200 # Estimate T2* via an oscilliator function circs_osc, xdata, omega = t2star_circuits(num_of_gates, gate_time, qubits, 5) backend_result = qiskit.execute(circs_osc, 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, omega
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, seed_simulator=SEED, backend_options={ 'max_parallel_experiments': 0 }, noise_model=noise_model, optimization_level=0).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 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, 30)).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 = 300 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) fit = T2Fitter(backend_result, xdata, qubits, fit_p0=[initial_a, initial_t2, initial_c], fit_bounds=([0, 0, -1], [2, expected_t2*1.2, 1])) self.assertAlmostEqual(fit.time(qid=0), expected_t2, delta=5, msg='Calculated T2 is inaccurate') self.assertTrue( fit.time_err(qid=0) < 5, 'Confidence in T2 calculation is too low: ' + str(fit.time_err))
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 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
# End of simoulation with noise (depolarizing_error) # Start of Simulation with noise (thermal_relaxation_error) # Start Noise Configuration noise_model2 = NoiseModel() #Add T1/T2 noise to the simulation t1 = 100. t2 = 80. gate1Q = 0.1 gate2Q = 0.5 noise_model2.add_all_qubit_quantum_error( thermal_relaxation_error(t1, t2, gate1Q), 'u2') noise_model2.add_all_qubit_quantum_error( thermal_relaxation_error(t1, t2, 2 * gate1Q), 'u3') noise_model2.add_all_qubit_quantum_error( thermal_relaxation_error(t1, t2, gate2Q).tensor( thermal_relaxation_error(t1, t2, gate2Q)), 'cx') # End Noise configuration job = execute(circuit, backend_sim, noise_model=noise_model2, basis_gates=['u1', 'u2', 'u3', 'cx'], shots=4096) result2 = job.result()
ideal_results.append( execute(qv_circs_nomeas[trial], backend=backend, optimization_level=0).result()) qv_fitter = qv.QVFitter(qubit_lists=qubit_lists) qv_fitter.add_statevectors(ideal_results) # define noise model n = 5 T1 = [j * 20 for j in range(1, n + 1)] T2 = [2 * t1 for t1 in T1] time_measure = 10 noise_model = NoiseModel() for j in range(n): noise_model.add_quantum_error( thermal_relaxation_error(T1[j], T2[j], time_measure), "measure", [j]) backend = Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] # use U,CX for now shots = 1024 exp_results = [] for trial in range(ntrials): print('Running trial %d' % trial) exp_results.append( qiskit.execute(qv_circs[trial], basis_gates=basis_gates, backend=backend, noise_model=noise_model, backend_options={ 'max_parallel_experiments': 0 }).result())
def test_t2star(self): """ Run the simulator with thermal relaxation noise. Then verify that the calculated T2star matches the t2 parameter. """ # Setting parameters num_of_gates = num_of_gates = np.append( (np.linspace(10, 150, 10)).astype(int), (np.linspace(160, 450, 5)).astype(int)) gate_time = 0.1 qubits = [0] expected_t2 = 10 error = thermal_relaxation_error(np.inf, expected_t2, gate_time, 0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') backend = qiskit.Aer.get_backend('qasm_simulator') shots = 200 # Estimating T2* via an exponential function circs, xdata, _ = t2star_circuits(num_of_gates, gate_time, qubits) 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() initial_t2 = expected_t2 initial_a = 0.5 initial_c = 0.5 T2Fitter(backend_result, xdata, qubits, fit_p0=[initial_a, initial_t2, initial_c], fit_bounds=([-0.5, 0, -0.5], [1.5, expected_t2 * 1.2, 1.5]), circbasename='t2star') # Estimate T2* via an oscilliator function circs_osc, xdata, omega = t2star_circuits(num_of_gates, gate_time, qubits, 5) backend_result = qiskit.execute(circs_osc, backend, shots=shots, seed_simulator=SEED, backend_options={ 'max_parallel_experiments': 0 }, noise_model=noise_model, optimization_level=0).result() initial_a = 0.5 initial_c = 0.5 initial_f = omega initial_phi = 0 T2StarFitter( backend_result, xdata, qubits, fit_p0=[initial_a, initial_t2, initial_f, initial_phi, initial_c], fit_bounds=([-0.5, 0, omega - 0.02, -np.pi, -0.5], [1.5, expected_t2 * 1.2, omega + 0.02, np.pi, 1.5]))
def test_thermal_relaxation_error_t1_t2_inf_ideal(self): """Test t1 = t2 = inf returns identity channel""" error = thermal_relaxation_error(np.inf, np.inf, 0) self.assertTrue(error.ideal(), msg="Non-identity channel")
def test_thermal_relaxation_error_zero_time_ideal(self): """Test gate_time = 0 returns identity channel""" error = thermal_relaxation_error(2, 3, 0) self.assertTrue(error.ideal(), msg="Non-identity channel")
def test_thermal_relaxation_error_raises_invalid_t1_t2(self): """Test raises error for invalid t2 > 2 * t1 parameters""" # T2 > 2 * T1 self.assertRaises(NoiseError, lambda: thermal_relaxation_error(1, 2.1, 0))
def test_t2star(self): """ Run the simulator with thermal relaxation noise. Then verify that the calculated T2star matches the t2 parameter. """ # Setting parameters num_of_gates = num_of_gates = np.append( (np.linspace(10, 150, 30)).astype(int), (np.linspace(160, 450, 20)).astype(int)) gate_time = 0.1 qubits = [0] expected_t2 = 10 error = thermal_relaxation_error(np.inf, expected_t2, gate_time, 0.5) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') backend = qiskit.Aer.get_backend('qasm_simulator') shots = 300 # Estimating T2* via an exponential function circs, xdata, _ = t2star_circuits(num_of_gates, gate_time, qubits) 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 = 0.5 initial_c = 0.5 fit = T2Fitter(backend_result, xdata, qubits, fit_p0=[initial_a, initial_t2, initial_c], fit_bounds=([-0.5, 0, -0.5], [1.5, expected_t2*1.2, 1.5]), circbasename='t2star') self.assertAlmostEqual(fit.time(qid=0), expected_t2, delta=2, msg='Calculated T2 is inaccurate') self.assertTrue( fit.time_err(qid=0) < 2, 'Confidence in T2 calculation is too low: ' + str(fit.time_err)) # Estimate T2* via an oscilliator function circs_osc, xdata, omega = t2star_circuits(num_of_gates, gate_time, qubits, 5) backend_result = qiskit.execute( circs_osc, backend, shots=shots, backend_options={'max_parallel_experiments': 0}, noise_model=noise_model).result() initial_a = 0.5 initial_c = 0.5 initial_f = omega initial_phi = 0 fit = T2StarFitter(backend_result, xdata, qubits, fit_p0=[initial_a, initial_t2, initial_f, initial_phi, initial_c], fit_bounds=([-0.5, 0, omega-0.02, -np.pi, -0.5], [1.5, expected_t2*1.2, omega+0.02, np.pi, 1.5])) self.assertAlmostEqual(fit.time(qid=0), expected_t2, delta=2, msg='Calculated T2 is inaccurate') self.assertTrue( fit.time_err(qid=0) < 2, 'Confidence in T2 calculation is too low: ' + str(fit.time_err))