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 = 1000 # 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_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 pauli_gate_error_noise_models(): """Local Pauli gate error noise models""" noise_models = [] # 100% all-qubit Pauli error on "id" gates error = pauli_error([('X', 1)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') noise_models.append(noise_model) # 25% all-qubit Pauli error on "id" gates error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'id') noise_models.append(noise_model) # 100% Pauli error on "id" gates on qubit-1 error = pauli_error([('X', 1)]) noise_model = NoiseModel() noise_model.add_quantum_error(error, 'id', [1]) noise_models.append(noise_model) # 25% all-qubit Pauli error on "id" gates on qubit-0 error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_quantum_error(error, 'id', [0]) noise_models.append(noise_model) # 25% Pauli-X error on spectator for CX gate on [0, 1] 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]) noise_models.append(noise_model) return noise_models
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.18 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 test_nonlocal_pauli_error_reset_25percent(self): """Test 25% Pauli-X error on qubit-1 when reseting qubit 0""" qr = QuantumRegister(2, 'qr') cr = ClassicalRegister(2, 'cr') circuit = QuantumCircuit(qr, cr) circuit.reset(qr[1]) circuit.barrier(qr) circuit.reset(qr[0]) 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_nonlocal_quantum_error(error, 'reset', [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_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 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_nonlocal_quantum_errors(self): qr = QuantumRegister(3, 'qr') circuit = QuantumCircuit(qr) circuit.x(qr[0]) circuit.x(qr[2]) error_x = pauli_error([('Y', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_nonlocal_quantum_error(error_x, 'x', [0], [1]) target_circuit = QuantumCircuit(qr) target_circuit.x(qr[0]) target_circuit.append(error_x.to_instruction(), [qr[1]]) target_circuit.x(qr[2]) result_circuit = insert_noise(circuit, noise_model) self.assertEqual(target_circuit, result_circuit)
def test_noise_models_equal(self): """Test two noise models are Equal""" roerror = [[0.9, 0.1], [0.5, 0.5]] error1 = pauli_error([['X', 1]], standard_gates=False) error2 = pauli_error([['X', 1]], standard_gates=True) model1 = NoiseModel() model1.add_all_qubit_quantum_error(error1, ['u3'], False) model1.add_quantum_error(error1, ['u3'], [2], False) model1.add_nonlocal_quantum_error(error1, ['cx'], [0, 1], [3], False) model1.add_all_qubit_readout_error(roerror, False) model1.add_readout_error(roerror, [0], False) model2 = NoiseModel() model2.add_all_qubit_quantum_error(error2, ['u3'], False) model2.add_quantum_error(error2, ['u3'], [2], False) model2.add_nonlocal_quantum_error(error2, ['cx'], [0, 1], [3], False) model2.add_all_qubit_readout_error(roerror, False) model2.add_readout_error(roerror, [0], False) self.assertEqual(model1, model2)
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 test_noise_model_noise_qubits(self): """Test noise instructions""" model = NoiseModel() target = [] self.assertEqual(model.noise_qubits, target) # Check adding a default error isn't added to noise qubits model = NoiseModel() model.add_all_qubit_quantum_error(pauli_error([['XX', 1]]), ['label'], False) target = [] self.assertEqual(model.noise_qubits, target) # Check adding a local error adds to noise qubits model = NoiseModel() model.add_quantum_error(pauli_error([['XX', 1]]), ['label'], [1, 0], False) target = sorted([0, 1]) self.assertEqual(model.noise_qubits, target) # Check adding a non-local error adds to noise qubits model = NoiseModel() with self.assertWarns(DeprecationWarning): model.add_nonlocal_quantum_error(pauli_error([['XX', 1]]), ['label'], [0], [1, 2], False) target = sorted([0, 1, 2]) self.assertEqual(model.noise_qubits, target) # Check adding a default error isn't added to noise qubits model = NoiseModel() model.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]], False) target = [] self.assertEqual(model.noise_qubits, target) # Check adding a local error adds to noise qubits model = NoiseModel() model.add_readout_error([[0.9, 0.1], [0, 1]], [2], False) target = [2] self.assertEqual(model.noise_qubits, target)
def test_noise_models_equal(self): """Test two noise models are Equal""" roerror = [[0.9, 0.1], [0.5, 0.5]] error1 = kraus_error([np.diag([1, 0]), np.diag([0, 1])]) error2 = pauli_error([("I", 0.5), ("Z", 0.5)]) model1 = NoiseModel() model1.add_all_qubit_quantum_error(error1, ['u3'], False) model1.add_quantum_error(error1, ['u3'], [2], False) with self.assertWarns(DeprecationWarning): model1.add_nonlocal_quantum_error(error1, ['cx'], [0, 1], [3], False) model1.add_all_qubit_readout_error(roerror, False) model1.add_readout_error(roerror, [0], False) model2 = NoiseModel() model2.add_all_qubit_quantum_error(error2, ['u3'], False) model2.add_quantum_error(error2, ['u3'], [2], False) with self.assertWarns(DeprecationWarning): model2.add_nonlocal_quantum_error(error2, ['cx'], [0, 1], [3], False) model2.add_all_qubit_readout_error(roerror, False) model2.add_readout_error(roerror, [0], False) self.assertEqual(model1, model2)
def pauli_reset_error_noise_models(): """Local Pauli reset error noise models""" noise_models = [] # 25% all-qubit Pauli error on reset error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'reset') noise_models.append(noise_model) # 25% local Pauli error on reset of qubit 1 error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_quantum_error(error, 'reset', [1]) noise_models.append(noise_model) # 25 % non-local Pauli error on qubit 1 for reset of qubit-0 error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_nonlocal_quantum_error(error, 'reset', [0], [1]) noise_models.append(noise_model) return noise_models
def pauli_measure_error_noise_models(): """Local Pauli measure error noise models""" noise_models = [] # 25% all-qubit Pauli error on measure error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, 'measure') noise_models.append(noise_model) # 25% local Pauli error on measure of qubit 1 error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_quantum_error(error, 'measure', [1]) noise_models.append(noise_model) # 25 % non-local Pauli error on qubit 1 for measure of qubit-1 error = pauli_error([('X', 0.25), ('I', 0.75)]) noise_model = NoiseModel() noise_model.add_nonlocal_quantum_error(error, 'measure', [0], [1]) noise_models.append(noise_model) return noise_models
print(noise_model) # ### Non-local qubit quantum error # # This applies an error to a specific set of noise qubits after any occurrence of an instruction acting on a specific of gate qubits. # # It is added as `noise_model.add_quantum_error(error, instructions, instr_qubits, error_qubits)`: # In[11]: # Create an empty noise model noise_model = NoiseModel() # Add depolarizing error on qubit 2 forall single qubit u1, u2, u3 gates on qubit 0 error = depolarizing_error(0.05, 1) noise_model.add_nonlocal_quantum_error(error, ['u1', 'u2', 'u3'], [0], [2]) # Print noise model info print(noise_model) # ### Executing a noisy simulation with a noise model # # * To execute a noisy simulation we pass the noise model object to `QasmSimulator.run` or `execute` using the `noise_model` kwarg. # * Eg: `qiskit.execute(circuits, QasmSimulator(), noise_model=noise)` # # **Important:** *When running a noisy simulation make sure you compile your `Qobj` to the same basis gates as the noise model!* # # This can be done using `NoiseModel.basis_gates` # # Noise Model Examples #
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()] # Fits data to an oscillation plt.figure(figsize=(10, 6)) a, phi, c = 1, -np.pi/20, 0 #?: what are these? fitting_parameter_bias = [a, oscillation_freq, phi, c] parameter_lower_bounds = [-0.5, 0, -np.pi, -0.5]