def test_ampcal1Q(self): """ Run the amplitude cal circuit generation and through the simulator to make sure there are no errors """ self._circs, xdata = ampcal_1Q_circuits(self._maxrep, self._qubits) # Set the simulator # Add a rotation error err_unitary = np.zeros([2, 2], dtype=complex) angle_err = 0.1 for i in range(2): err_unitary[i, i] = np.cos(angle_err) err_unitary[i, (i+1) % 2] = np.sin(angle_err) err_unitary[0, 1] *= -1.0 error = coherent_unitary_error(err_unitary) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'u2') initial_theta = 0.18 initial_c = 0.5 fit = AmpCalFitter(self.run_sim(noise_model), xdata, self._qubits, fit_p0=[initial_theta, initial_c], fit_bounds=([-np.pi, -1], [np.pi, 1])) self.assertAlmostEqual(fit.angle_err(0), 0.1, 2)
def test_ampcalCX(self): """ Run the amplitude cal circuit generation for CX and through the simulator to make sure there are no errors """ self._circs, xdata = ampcal_cx_circuits(self._maxrep, self._qubits, self._controls) err_unitary = np.eye(4, dtype=complex) angle_err = 0.15 for i in range(2): err_unitary[2 + i, 2 + i] = np.cos(angle_err) err_unitary[2 + i, 2 + (i + 1) % 2] = -1j * np.sin(angle_err) error = coherent_unitary_error(err_unitary) noise_model = NoiseModel() noise_model.add_nonlocal_quantum_error(error, 'cx', [1, 0], [0, 1]) initial_theta = 0.02 initial_c = 0.5 fit = AmpCalCXFitter(self.run_sim(noise_model), xdata, self._qubits, fit_p0=[initial_theta, initial_c], fit_bounds=([-np.pi, -1], [np.pi, 1])) self.assertAlmostEqual(fit.angle_err(0), 0.15, 2) self.assertAlmostEqual(fit.angle_err(1), 0.0, 2)
def rb_purity_circuit_execution(rb_opts: dict, shots: int): """ Create purity rb circuits with depolarizing 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 purity circuits simulations list: the xdata of the rb circuit int: npurity (3^(number of qubits)) list: list of Results of the coherent circuits simulations """ # Load simulator backend = qiskit.Aer.get_backend('qasm_simulator') basis_gates = ['u1', 'u2', 'u3', 'cx'] rb_purity_circs, xdata, npurity = rb.randomized_benchmarking_seq(**rb_opts) noise_model = create_depolarizing_noise_model() # coherent noise err_unitary = np.zeros([2, 2], dtype=complex) angle_err = 0.1 for i in range(2): err_unitary[i, i] = np.cos(angle_err) err_unitary[i, (i + 1) % 2] = np.sin(angle_err) err_unitary[0, 1] *= -1.0 error = coherent_unitary_error(err_unitary) coherent_noise_model = NoiseModel() coherent_noise_model.add_all_qubit_quantum_error(error, 'u3') purity_results = [] coherent_results = [] for circuit_list in rb_purity_circs: for purity_num in range(npurity): current_circ = circuit_list[purity_num] # non-coherent purity results purity_results.append( qiskit.execute(current_circ, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=noise_model, seed_simulator=SEED).result()) # coherent purity results # THE FITTER IS NOT TESTED YET coherent_results.append( qiskit.execute(current_circ, backend=backend, basis_gates=basis_gates, shots=shots, noise_model=coherent_noise_model, seed_simulator=SEED).result()) return purity_results, xdata, npurity, coherent_results
def test_zz(self): """ Run the simulator with unitary noise. Then verify that the calculated ZZ matches the zz parameter. """ num_of_gates = np.arange(0, 60, 10) gate_time = 0.1 qubits = [0] spectators = [1] # Generate experiments circs, xdata, osc_freq = zz_circuits(num_of_gates, gate_time, qubits, spectators, nosc=2) # Set the simulator with ZZ zz_expected = 0.1 zz_unitary = np.eye(4, dtype=complex) zz_unitary[3, 3] = np.exp(1j * 2 * np.pi * zz_expected * gate_time) error = coherent_unitary_error(zz_unitary) noise_model = NoiseModel() noise_model.add_nonlocal_quantum_error(error, 'id', [0], [0, 1]) # Run the simulator backend = qiskit.Aer.get_backend('qasm_simulator') shots = 100 # For demonstration purposes split the execution into two jobs backend_result = qiskit.execute(circs, backend, shots=shots, seed_simulator=SEED, noise_model=noise_model, optimization_level=0).result() initial_a = 0.5 initial_c = 0.5 initial_f = osc_freq initial_phi = 0.0 ZZFitter(backend_result, xdata, qubits, spectators, fit_p0=[initial_a, initial_f, initial_phi, initial_c], fit_bounds=([-0.5, 0, -np.pi, -0.5], [1.5, 2 * osc_freq, np.pi, 1.5]))
def zz_circuit_execution() -> Tuple[qiskit.result.Result, np.array, List[int], List[int], float, float]: """ Create ZZ circuits and simulate them. Returns: * Backend result. * xdata. * Qubits for the ZZ measurement. * Spectators. * ZZ parameter that used in the circuit creation * Frequency. """ num_of_gates = np.arange(0, 60, 10) gate_time = 0.1 qubits = [0] spectators = [1] # Generate experiments circs, xdata, omega = zz_circuits(num_of_gates, gate_time, qubits, spectators, nosc=2) # Set the simulator with ZZ zz_value = 0.1 zz_unitary = np.eye(4, dtype=complex) zz_unitary[3, 3] = np.exp(1j*2*np.pi*zz_value*gate_time) error = coherent_unitary_error(zz_unitary) noise_model = NoiseModel() noise_model.add_nonlocal_quantum_error(error, 'id', [0], [0, 1]) # Run the simulator backend = qiskit.Aer.get_backend('qasm_simulator') shots = 100 backend_result = qiskit.execute(circs, backend, shots=shots, seed_simulator=SEED, noise_model=noise_model, optimization_level=0).result() return backend_result, xdata, qubits, spectators, zz_value, omega
def test_coherent_unitary_error(self): """Test coherent unitary error""" unitary = np.diag([1, -1, 1, -1]) error = coherent_unitary_error(unitary) ref = mixed_unitary_error([(unitary, 1)]) self.assertEqual(error.as_dict(), ref.as_dict())
## Ammplitude Error Characterization for Single Qubit Gates # Measures amplitude error, in the pi/2 pulse, on qubits 2 and 4 qubits = [4, 2] max_circuit_repetitions = 10 circuits, delay_times = ampcal_1Q_circuits(max_circuit_repetitions, qubits) # Sets simulator and adds rotation error. angle_error = 0.1 error_unitary_matrix = np.zeros([2,2], dtype=complex) for i in range(2): error_unitary_matrix[i, i] = np.cos(angle_error) error_unitary_matrix[i, (i+1) % 2] = np.sin(angle_error) error_unitary_matrix[0, 1] *= -1.0 error = coherent_unitary_error(error_unitary_matrix) my_noise_model = NoiseModel() my_noise_model.add_all_qubit_quantum_error(error, 'u2') # Runs simulator backend = Aer.get_backend('qasm_simulator') trials = 500 result = execute(circuits, backend, shots=trials, noise_model=my_noise_model).result() # FIts data to an oscillation plt.figure(figsize=(10,6)) theta, c = 0.02, 0.5 # ?: again, what are these parameter values? initial_parameter_bias = [theta, c] parameter_lower_bounds = [-np.pi, -1] parameter_upper_bounds = [np.pi, 1]
# Generates experiments. oscillations_count = 2 circuits, delay_times, oscillation_freq = zz_circuits(number_of_gates, gate_time, qubits, spectators, nosc = oscillations_count) ## Splits circuits into multiple jobs, giving results to fitter as a list. # Sets simulator with ZZ unknown_factor = 0.02 # ?: what is this? ground state energy frequency (E=hw)? time_evolver = np.exp(1j * 2 * np.pi * unknown_factor * gate_time) zz_unitary = np.eye(4, dtype=complex) zz_unitary[3,3] = time_evolver error = coherent_unitary_error(zz_unitary) # for applying to qubits in noise # model below my_noise_model = NoiseModel() error_instructions = 'id' noise_qubits = qubits + spectators my_noise_model.add_nonlocal_quantum_error(error, error_instructions, qubits, noise_qubits) # Runs the simulator. backend = Aer.get_backend('qasm_simulator') trials = 500 print("Running circuits, in two batches of 20 each...") results = [execute(circuits[:20], backend, shots=trials, noise_model=my_noise_model).result(), execute(circuits[20:], backend, shots=trials, noise_model=my_noise_model).result()]