def noise_model_bit(error, measure=True, thermal=True): """ Creates error for bit flip :param error: Probability of happening a bitflip :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() flip_error = error cnot_error = 2 * error bit_flip = pauli_error([('X', flip_error), ('I', 1 - flip_error)]) cnot_flip = pauli_error([('X', cnot_error), ('I', 1 - cnot_error)]) cnot_error = cnot_flip.tensor(cnot_flip) noise_model.add_all_qubit_quantum_error(bit_flip, ["u1", "u2", "u3"]) noise_model.add_all_qubit_quantum_error(cnot_error, ['cx'], warnings=False) 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
def noise_model_depolarizing(error, measure=True, thermal=True): """ Creates error for depolarizing channel :param error: Probability of depolarizing channel :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() depolarizing = depolarizing_error(error, 1) cdepol_error = depolarizing_error(2 * error, 2) noise_model.add_all_qubit_quantum_error(cdepol_error, ['cx'], warnings=False) noise_model.add_all_qubit_quantum_error(depolarizing, ["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
def noise_model_phase(error, measure=True, thermal=True): """ Creates error for phase flip :param error: Probability of happening a phaseflip :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']) phase_error = error cz_error = 2 * error phase_flip = pauli_error([('Z', phase_error), ('I', 1 - phase_error)]) cz_error = pauli_error([('Z', cz_error), ('I', 1 - cz_error)]) cz_error = cz_error.tensor(cz_error) noise_model.add_all_qubit_quantum_error(cz_error, ['cx'], warnings=False) noise_model.add_all_qubit_quantum_error(phase_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
def generateRelaxationError(machine, gate, qubits, t1, t2, amp=1, custom_t=False): """ Return a relaxation error """ if len(qubits) == 1: try: if (not custom_t): t1 = machine.properties().t1(qubits[0]) t2 = min(machine.properties().t2(qubits[0]), 2 * t1) t1 = t1 / amp t2 = t2 / amp gate_time = machine.properties().gate_length(gate, qubits) error = thermal_relaxation_error(t1, t2, gate_time) return error except: return None else: try: #setting times if (custom_t): t1_a = t1 t2_a = min(t2, 2 * t1) t1_b = t1_a t2_b = t2_a else: t1_a = machine.properties().t1(qubits[0]) t2_a = min(machine.properties().t2(qubits[0]), 2 * t1_a) t1_b = machine.properties().t1(qubits[1]) t2_b = min(machine.properties().t2(qubits[1]), 2 * t1_b) t1_a = t1_a / amp t2_a = t2_a / amp t1_b = t1_b / amp t2_b = t2_b / amp #finding gate time time_cx = machine.properties().gate_length(gate, qubits) error = thermal_relaxation_error(t1_a, t2_a, time_cx).expand( thermal_relaxation_error(t1_b, t2_b, time_cx)) return error except: return None
def bonet23(backend, p=0.01, q=0.01, T1=20000, T2=20000, t=20): #times in ns if backend.name() == 'qasm_simulator': #dephasing component #A0 = np.sqrt(p)*np.array([[1,0], [0,0]]) #A1 = np.sqrt(p)*np.array([[0,0], [0,1]]) #A2 = np.sqrt(1-p)*np.array([[1,0], [0,1]]) #kraus_ops = [A0, A1, A2] #dephase_error = noise.kraus_error(kraus_ops) #noise_model.add_all_qubit_quantum_error(dephase_error, qasm_1qubit_gates) #readout component error_bitflip = noise.errors.pauli_error([('X', q), ('I', 1 - q)]) noise_model.add_all_qubit_quantum_error(error_bitflip, 'measure') #thermal component thermal_error = noise.thermal_relaxation_error(T1, T2, t) noise_model.add_all_qubit_quantum_error(thermal_error, qasm_1qubit_gates) else: print('SimulationError: Invalid option for noisy simulator') sys.exit() return noise_model
def test_ops_types(self): """Test adding noises only to delays in a scheduled circuit.""" t1s = [0.10, 0.11] t2s = [0.20, 0.21] dt = 0.01 qc = QuantumCircuit(2, 2) qc.h(0) qc.cx(0, 1) qc.measure([0, 1], [0, 1]) durations = [("h", None, 10), ("cx", None, 50), ("measure", None, 200)] sched_circ = transpile(qc, scheduling_method='alap', instruction_durations=durations) noise_pass = RelaxationNoisePass(t1s=t1s, t2s=t2s, dt=dt, op_types=Delay) noisy_circ = noise_pass(sched_circ) expected = QuantumCircuit(2, 2) expected.h(0) expected.delay(10, 1) expected.append( thermal_relaxation_error(t1s[1], t2s[1], 10 * dt).to_instruction(), [1]) expected.cx(0, 1) expected.measure([0, 1], [0, 1]) self.assertEqual(expected, noisy_circ)
def get_data_point(circ, theta, phi, lam, readout_params, depol_param, thermal_params, shots): """Generate a dict datapoint with the given circuit and noise parameters""" U3_gate_length = 7.111111111111112e-08 # extract parameters (p0_0, p1_0), (p0_1, p1_1) = readout_params depol_prob = depol_param t1, t2, population = thermal_params # Add Readout and Quantum Errors noise_model = NoiseModel() noise_model.add_all_qubit_readout_error(ReadoutError(readout_params)) noise_model.add_all_qubit_quantum_error(depolarizing_error(depol_param, 1), 'u3', warnings=False) noise_model.add_all_qubit_quantum_error( thermal_relaxation_error(t1, t2, U3_gate_length, excited_state_population=population), 'u3', warnings=False) job = execute(circ, QasmSimulator(), shots=shots, noise_model=noise_model) result = job.result() # add data point to DataFrame data_point = {'theta': theta, 'phi': phi, 'lam': lam, 'p0_0': p0_0, 'p1_0': p1_0, 'p0_1': p0_1, 'p1_1': p1_1, 'depol_prob': depol_prob, 't1': t1, 't2': t2, 'population': population, 'E': result.get_counts(0).get('1', 0) / shots} return data_point
def test_delay_circuit_on_multi_qubits(self): t1s = [0.10, 0.11] t2s = [0.20, 0.21] dt = 0.01 duration = 100 qc = QuantumCircuit(2) qc.delay(duration, 0) qc.delay(duration, 1) relax_pass = RelaxationNoisePass(t1s=t1s, t2s=t2s, dt=dt) actual = qi.SuperOp(relax_pass(qc)) noise0 = thermal_relaxation_error(t1s[0], t2s[0], duration * dt) noise1 = thermal_relaxation_error(t1s[1], t2s[1], duration * dt) expected = qi.SuperOp(noise0.expand(noise1)) self.assertEqual(expected, actual)
def noise_model_thermal(error, measure=False, thermal=False): """ Creates error for thermal relaxation for T1, T2 = 1.5, 1.2 :param error: time of gate. Normalized to be equal to the error in all other functions :param measure: True or False, whether we include readout errors with probability 10 * error :return: noise model for the error """ noise_model = NoiseModel() 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"]) 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) return noise_model
def randbin3(data, F): # simulator --- WORKS with reversed probabilities #error_1 = noise.depolarizing_error(data.phase_err, 1) error_1 = noise.phase_amplitude_damping_error(param_phase=data.phase_err, param_amp=data.amp_err) error_2 = noise.thermal_relaxation_error(data.T_1, data.T_2, data.t * 10**(6), excited_state_population=0) noise_model = noise.NoiseModel() noise_model.add_all_qubit_quantum_error(error_2, ['rz']) basis_gates = noise_model.basis_gates phi = data.const * F * data.t * data.F_degree q = QuantumRegister(1) c = ClassicalRegister(1) # Create a Quantum Circuit acting on the q register circuit = QuantumCircuit(q, c) circuit.h(q) circuit.rz(phi, q) circuit.h(q) # Map the quantum measurement to the classical bits circuit.measure(q, c) # Execute the circuit on the qasm simulator job = execute(circuit, simulator, basis_gates=basis_gates, noise_model=noise_model, shots=data.num_of_repetitions) #job_monitor(job) # Grab results from the job result = job.result() # Returns counts counts = result.get_counts(circuit) #print(counts, 'for sim') #return counts try: if counts['1'] < counts['0']: # LOWEST BECAUSE PROBS ARE REVERSED!!! return 1 else: return 0 except Exception: return abs(int(list(counts.keys())[0]) - 1)
def thermal(backend, T1=20000, T2=20000, t=20): #times in ns if backend.name() == 'qasm_simulator': thermal_error = noise.thermal_relaxation_error(T1, T2, t) noise_model.add_all_qubit_quantum_error(thermal_error, qasm_1qubit_gates) else: print('SimulationError: Invalid option for noisy simulator') sys.exit() return noise_model
def test_delay_circuit_on_single_qubit(self): t1s = [0.10, 0.11] t2s = [0.20, 0.21] dt = 0.01 duration = 100 qc = QuantumCircuit(1) qc.delay(duration, 0) relax_pass = RelaxationNoisePass(t1s=t1s, t2s=t2s, dt=dt) actual = qi.SuperOp(relax_pass(qc)) expected = qi.SuperOp( thermal_relaxation_error(t1s[0], t2s[0], duration * dt)) self.assertEqual(expected, actual)
def test_delay_units(self, duration, unit): """Test un-scheduled delay with different units.""" t1 = 0.004 t2 = 0.008 dt = 1e-10 # 0.1 ns target_duration = 1e-5 qc = QuantumCircuit(1) qc.delay(duration, 0, unit=unit) relax_pass = RelaxationNoisePass(t1s=[t1], t2s=[t2], dt=dt) actual = qi.SuperOp(relax_pass(qc)) expected = qi.SuperOp(thermal_relaxation_error(t1, t2, target_duration)) self.assertEqual(expected, actual)
def get_data_point(theta, phi, lam, readout_params, depol_param, thermal_params, shots): """Generate a dict datapoint with the given circuit and noise parameters""" U3_gate_length = 7.111111111111112e-08 circ = QuantumCircuit(1, 1) circ.append(U3Gate(theta, phi, lam), [0]) circ.measure(0, 0) new_circ = qiskit.compiler.transpile(circ, basis_gates=['u3'], optimization_level=0) # extract parameters (p0_0, p1_0), (p0_1, p1_1) = readout_params depol_prob = depol_param t1, t2, population = thermal_params noise_model = NoiseModel() # Add Readout and Quantum Errors noise_model.add_all_qubit_readout_error(ReadoutError(readout_params)) noise_model.add_all_qubit_quantum_error(depolarizing_error(depol_param, 1), 'u3', warnings=False) noise_model.add_all_qubit_quantum_error(thermal_relaxation_error( t1, t2, U3_gate_length, population), 'u3', warnings=False) job = execute(circ, QasmSimulator(), shots=shots, noise_model=noise_model) result = job.result() # add data point to DataFrame data_point = { 'theta': theta, 'phi': phi, 'lam': lam, 'p0_0': p0_0, 'p1_0': p1_0, 'p0_1': p0_1, 'p1_1': p1_1, 'depol_prob': depol_prob, 't1': t1, 't2': t2, 'population': population, 'E': result.get_counts(0).get('0', 0) / shots } return data_point
T2s = np.array([min(T2s[j], 2 * T1s[j]) for j in range(numq)]) # Instruction times (in nanoseconds) time_h = 50 # u2(0,pi) time_t = 0 # u1(pi/4) time_tdg = 0 # u1(-pi/4) time_cx = 300 time_swap = 300 #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 = [ noise.thermal_relaxation_error(t1, t2, time_measure) for t1, t2 in zip(T1s, T2s) ] errors_h = [ noise.thermal_relaxation_error(t1, t2, time_h).compose(error_1) for t1, t2 in zip(T1s, T2s) ] errors_t = [ noise.thermal_relaxation_error(t1, t2, time_t).compose(error_1) for t1, t2 in zip(T1s, T2s) ] errors_tdg = [ noise.thermal_relaxation_error(t1, t2, time_tdg).compose(error_1) for t1, t2 in zip(T1s, T2s) ] errors_cx = [[
# Truncate random T2s <= T1s T2s = np.array([min(T2s[j], 2 * T1s[j]) for j in range(4)]) #%% # Instruction times (in nanoseconds) time_u1 = 0 # virtual gate time_u2 = 50 # (single X90 pulse) time_u3 = 100 # (two X90 pulses) time_cx = 300 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)] #%%
def __init__(self, coherent_error_angles: List[float] = None, depol_errors: List[float] = None, damping_params: List[float] = None, relaxation_params: List[float] = None, ): """Creating the noise model object and specifying the used gateset All erros are assigend uniformly to the set of single-/two-qubit gates Parameters ---------- coherent_error_angles : List[float] Angles of unitary over-rotaions for single, and two-qubit gates. depol_errors : List[float], optional Params of single- and two-qubit depolarization noise channels. damping_params : List[float] Amplitude damping, phase damping and excited state population of a general amplitude + phase damping channel. relaxation_params : List[float] t1 time, t2 time, timestep (unit of time) and excited state population of a thermal relaxation noise channel """ # create "empty" noise model from the original Qiskit class # Need to specify the basis of all qiskit prebuilt gates that should be # noisy super().__init__(basis_gates=['h', 't', 'x', 'y', 's', 'sdg', 'z', 'cx', 'cy', 'cz']) # list of qiskit labels of all single qubit gates specified in the HAL # For all single qubit gates defined used in the Qiskit HAL, the # corresponding label must be given here, so that errors are assigned # to that gate # Uppercase letters are custom gates defined in the Qiskit backend # while lowercase labels are default qiskit labels self._sq_gates = ['h', 'PiXY', 'PiYZ', 'PiZX', 'R', 'RX', 'RY', 'RZ', 's', 'sdg', 'sqrt_x', 'SX', 'SY', 't', 'X', 'Y', 'Z'] # list of qiskit labels of all two qubit gates specified in the HAL self._tq_gates = ['cx', 'cy', 'cz'] # defining matrix representations of Pauli gates self.gate2matrix_dict = { 'X': np.array([[0, 1], [1, 0]]) + 0j, 'Y': np.array([[0, -1j], [1j, 0]]), 'Z': np.array([[1, 0], [0, -1]]) + 0j, } # adding coherent overrotations to each single and two-qubit gate # using Qiskit's unitary error. This will be applied every time the # original gate is executed in a circuit if coherent_error_angles is not None: assert isinstance(coherent_error_angles, list), \ 'Need list of Single-qubit and two-qubit gate errors' assert len(coherent_error_angles) == 2 self.sq_angle, self.tq_angle = coherent_error_angles error_h = coherent_unitary_error( self._sq_rot(self.sq_angle, np.pi/4, 0)) self.add_all_qubit_quantum_error(error_h, ['h']) error_x = coherent_unitary_error( self._sq_rot(self.sq_angle, np.pi/2, 0)) self.add_all_qubit_quantum_error(error_x, ['X', 'RX', 'PiYZ', 'sqrt_x']) error_y = coherent_unitary_error( self._sq_rot(self.sq_angle, np.pi/2, np.pi/2)) self.add_all_qubit_quantum_error(error_y, ['Y', 'RY', 'PiZX']) error_z = coherent_unitary_error(self._sq_rot(self.sq_angle, 0, 0)) self.add_all_qubit_quantum_error(error_z, ['Z', 'RZ', 's', 'sdg', 't', 'PiXY']) error_sx = coherent_unitary_error( self._sq_rot(self.sq_angle, np.pi/2, np.pi/4)) self.add_all_qubit_quantum_error(error_sx, ['SX']) error_sy = coherent_unitary_error( self._sq_rot(self.sq_angle, np.pi/2, -np.pi/4)) self.add_all_qubit_quantum_error(error_sy, ['SY']) error_cx = coherent_unitary_error( self._contr_sq_rot(self.tq_angle, np.pi/2, 0)) self.add_all_qubit_quantum_error(error_cx, ['cx']) error_cy = coherent_unitary_error( self._contr_sq_rot(self.tq_angle, np.pi/2, np.pi/2)) self.add_all_qubit_quantum_error(error_cy, ['cy']) error_cz = coherent_unitary_error( self._contr_sq_rot(self.tq_angle, 0, 0)) self.add_all_qubit_quantum_error(error_cz, ['cz']) # adding a depolarization channel if depol_errors is not None: assert isinstance(depol_errors, list), \ 'Need list of Single-qubit and two-qubit depolarization params' assert len(depol_errors) == 2 self.sq_depol, self.tq_depol = depol_errors sq_depol_error = depolarizing_error(self.sq_depol, 1) self.add_all_qubit_quantum_error( sq_depol_error, self._sq_gates, warnings=False) tq_depol_error = depolarizing_error(self.tq_depol, 2) self.add_all_qubit_quantum_error( tq_depol_error, self._tq_gates, warnings=False) # adding an amplitude and/or phase damping channel if damping_params is not None: assert isinstance(damping_params, list), \ 'Need list of amplitude damping, phase damping and excited \ state population' assert len(damping_params) == 3 self.amp_param, self.phase_param, self.amp_pop = damping_params damping_error = phase_amplitude_damping_error(self.amp_param, self.phase_param, self.amp_pop) self.add_all_qubit_quantum_error( damping_error, self._sq_gates, warnings=False) # adding a themral relaxation error if relaxation_params is not None: assert isinstance(relaxation_params, list), \ 'Need list of t1 time, t2 time, gate time and ex. state pop.' assert len(relaxation_params) == 4 self.t1, self.t2, self.time, self.damp_pop = relaxation_params relaxation_error = thermal_relaxation_error(self.t1, self.t2, self.time, self.damp_pop) self.add_all_qubit_quantum_error( relaxation_error, self._sq_gates, warnings=False) # add the 'unitary' label to the basis set for all the custom gates # in the HAL specified as a UnitaryGate object self.add_basis_gates(['unitary'])