def test_reset_error_specific_qubit_50percent(self): """Test 50% perecent reset error on qubit-0""" # Test circuit: ideal outcome "11" qr = QuantumRegister(2, 'qr') cr = ClassicalRegister(2, 'cr') circuit = QuantumCircuit(qr, cr) circuit.x(qr) circuit.measure(qr, cr) backend = QasmSimulator() noise_circs = [[{ "name": "reset", "qubits": [0] }], [{ "name": "id", "qubits": [0] }]] # 50% reset noise on qubit-0 "u3" only. noise_probs = [0.5, 0.5] error = QuantumError(zip(noise_circs, noise_probs)) noise_model = NoiseModel() noise_model.add_quantum_error(error, "u3", [0]) shots = 2000 target = {'0x2': shots / 2, '0x3': shots / 2} circuit = transpile(circuit, basis_gates=noise_model.basis_gates) qobj = assemble([circuit], backend, shots=shots) 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_reset_error_specific_qubit_25percent(self): """Test 25% percent reset error on qubit-1""" # Test circuit: ideal outcome "11" qr = QuantumRegister(2, 'qr') cr = ClassicalRegister(2, 'cr') circuit = QuantumCircuit(qr, cr) circuit.x(qr) circuit.measure(qr, cr) backend = QasmSimulator() noise_circs = [[{ "name": "reset", "qubits": [0] }], [{ "name": "id", "qubits": [0] }]] # 25% reset noise on qubit-1 "u3" only. noise_probs = [0.25, 0.75] error = QuantumError(zip(noise_circs, noise_probs)) noise_model = NoiseModel() noise_model.add_quantum_error(error, "u3", [1]) shots = 1000 # target = {'01': shots / 4, '11': 3 * shots / 4} target = {'0x1': shots / 4, '0x3': 3 * 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 reset_gate_error_noise_models(): """Reset gate error noise models""" noise_models = [] # 50% reset to 0 state on qubit 0 error = reset_error(0.5) noise_model = NoiseModel() noise_model.add_quantum_error(error, 'x', [0]) noise_models.append(noise_model) # 25% reset to 0 state on qubit 1 error = reset_error(0.25) noise_model = NoiseModel() noise_model.add_quantum_error(error, 'x', [1]) noise_models.append(noise_model) # 100% reset error to 0 on all qubits error = reset_error(1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) noise_models.append(noise_model) # 100% reset error to 1 on all qubits error = reset_error(0, 1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) noise_models.append(noise_model) # 25% reset error to 0 and 1 on all qubits error = reset_error(0.25, 0.25) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['id', 'x']) noise_models.append(noise_model) return noise_models
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_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_raises_duplicate_qubits(self): """Test duplicate qubits raises exception""" model = NoiseModel() self.assertRaises(NoiseError, remap_noise_model, model, [[0, 1], [2, 1]], warnings=False) model = NoiseModel() error = depolarizing_error(0.5, 1) model.add_quantum_error(error, ['u3'], [2], False) self.assertRaises(NoiseError, remap_noise_model, model, [[3, 2]], warnings=False)
def thermal_noise(n_qubits, mu1, mu2, sig): num = n_qubits # T1 and T2 values for qubits 0-3 T1s = np.random.normal( mu1, sig, num) # Sampled from normal distribution mean 50 microsec T2s = np.random.normal( mu2, sig, num) # Sampled from normal distribution mean 50 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 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(num): 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(num): noise_thermal.add_quantum_error(errors_cx[j][k], "cx", [j, k]) return noise_thermal
def make_noise_model(dep_err_rate, ro_err_rate, qubits): # Define a noise model that applies uniformly to the given qubits model = NoiseModel() dep_err = depolarizing_error(dep_err_rate, 2) ro_err = ReadoutError( [[1 - ro_err_rate, ro_err_rate], [ro_err_rate, 1 - ro_err_rate]] ) # Add depolarising error to CX gates between any qubits (implying full connectivity) for i, j in product(qubits, repeat=2): model.add_quantum_error(dep_err, ["cx"], [i, j]) # Add readout error for each qubit for i in qubits: model.add_readout_error(ro_err, qubits=[i]) return model
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_from_dict(self): noise_ops_1q = [((IGate(), [0]), 0.9), ((XGate(), [0]), 0.1)] noise_ops_2q = [((PauliGate('II'), [0, 1]), 0.9), ((PauliGate('IX'), [0, 1]), 0.045), ((PauliGate('XI'), [0, 1]), 0.045), ((PauliGate('XX'), [0, 1]), 0.01)] noise_model = NoiseModel() with self.assertWarns(DeprecationWarning): noise_model.add_quantum_error(QuantumError(noise_ops_1q, 1), 'h', [0]) noise_model.add_quantum_error(QuantumError(noise_ops_1q, 1), 'h', [1]) noise_model.add_quantum_error(QuantumError(noise_ops_2q, 2), 'cx', [0, 1]) noise_model.add_quantum_error(QuantumError(noise_ops_2q, 2), 'cx', [1, 0]) deserialized = NoiseModel.from_dict(noise_model.to_dict()) self.assertEqual(noise_model, deserialized)
def test_specific_qubit_pauli_error_measure_25percent(self): """Test 25% Pauli-X error on measure of qubit-1""" qr = QuantumRegister(2, 'qr') cr = ClassicalRegister(2, 'cr') circuit = QuantumCircuit(qr, cr) 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_quantum_error(error, 'measure', [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 thermalRelaxationChannel(backend, T1s, T2s, graph, gates): '''Method that returns the thermal relaxation error quantum channel.''' graph = [[0,1], [1,2], [2,3]] # The two-qubit gates that we are interested in names = [0, 1, 2, 3] # The single-qubit gates that we are interested in # 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 test_remap_quantum_errors(self): """Test remapping of quantum errors.""" model = NoiseModel() error1 = depolarizing_error(0.5, 1) error2 = depolarizing_error(0.5, 2) model.add_quantum_error(error1, ['u3'], [0], False) model.add_quantum_error(error2, ['cx'], [1, 2], False) remapped_model = remap_noise_model(model, [[0, 1], [1, 2], [2, 0]], warnings=False) target = NoiseModel() target.add_quantum_error(error1, ['u3'], [1], False) target.add_quantum_error(error2, ['cx'], [2, 0], False) self.assertEqual(remapped_model, target)
def test_transform_noise(self): org_error = reset_error(0.2) new_error = pauli_error([("I", 0.5), ("Z", 0.5)]) model = NoiseModel() model.add_all_qubit_quantum_error(org_error, ['x']) model.add_quantum_error(org_error, ['sx'], [0]) model.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]]) def map_func(noise): return new_error if noise == org_error else None actual = transform_noise_model(model, map_func) expected = NoiseModel() expected.add_all_qubit_quantum_error(new_error, ['x']) expected.add_quantum_error(new_error, ['sx'], [0]) expected.add_all_qubit_readout_error([[0.9, 0.1], [0, 1]]) self.assertEqual(actual, expected)
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_specific_qubit_pauli_error_gate_100percent(self): """Test 100% Pauli error on id gates on qubit-1""" 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_quantum_error(error, 'id', [1]) # Execute target = {'0x2': 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_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 = 2000 # 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} circuit = transpile(circuit, basis_gates=noise_model.basis_gates) qobj = assemble([circuit], backend, shots=shots) 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_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 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 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
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 thermal_noise(mu1, mu2, sigma, p, t): num = 2 + (2 * t - 1) * p # T1 and T2 values for qubits 0-num T1s = np.random.normal( mu1, sigma, num ) # Sampled from normal distribution mean mu1 microsec, sigma = 10e3 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 #Time to implement controlled/controlled-controlled evolution unitary #Done this way to save on computational cost of simulating noise. time_cu = 2 * (2 * time_u1 + 2 * time_cx + 2 * time_u3 ) #time is 2* cu3(1). time_ccu = 3 * ( 2 * time_u1 + 2 * time_cx + 2 * time_u3 ) + 2 * time_cx #ccu(2) can be decomposed into 2 ccu(1) which can be decomposed to 3 cu3(1) and 2 cx gates # 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)] errors_cu = [[[ thermal_relaxation_error(t1a, t2a, time_cu).expand( thermal_relaxation_error(t1b, t2b, time_cu).expand( thermal_relaxation_error(t1c, t2c, time_cu))) for t1a, t2a in zip(T1s, T2s) ] for t1b, t2b in zip(T1s, T2s)] for t1c, t2c in zip(T1s, T2s)] if num > 5: #i.e. more than 1 iteration of PEA errors_ccu = [[[[ thermal_relaxation_error(t1a, t2a, time_ccu).expand( thermal_relaxation_error(t1b, t2b, time_ccu).expand( thermal_relaxation_error(t1c, t2c, time_ccu).expand( thermal_relaxation_error(t1d, t2d, time_ccu)))) for t1a, t2a in zip(T1s, T2s) ] for t1b, t2b in zip(T1s, T2s)] for t1c, t2c in zip(T1s, T2s)] for t1d, t2d 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]) #Only Need CU and CCU gate noise between qubits that actually implement this gate! state_qubit_0 = num - 2 state_qubit_1 = num - 1 for q in range( t ): #CU only acts on state qubits and register from first round (in this order) noise_thermal.add_quantum_error( errors_cu[state_qubit_1][state_qubit_0][q], "cu", [state_qubit_1, state_qubit_0, q]) if num > 5: for j in range( 1, p ): #CCU acts on state qubits, or qubit from previous round and fresh register qubits (in this order) for q in range(t * j, t * (j + 1)): noise_thermal.add_quantum_error( errors_ccu[state_qubit_1][state_qubit_0][p * t + (j - 1)][q], "ccu", [state_qubit_1, state_qubit_0, p * t + (j - 1), q]) noise_thermal.add_basis_gates(['unitary']) return noise_thermal
def _get_noise_model_from_backend_v2( self, gate_error=True, readout_error=True, thermal_relaxation=True, temperature=0, gate_lengths=None, gate_length_units="ns", standard_gates=None, ): """Build noise model from BackendV2. This is a temporary fix until qiskit-aer supports building noise model from a BackendV2 object. """ from qiskit.circuit import Delay from qiskit.providers.exceptions import BackendPropertyError from qiskit.providers.aer.noise import NoiseModel from qiskit.providers.aer.noise.device.models import ( _excited_population, basic_device_gate_errors, basic_device_readout_errors, ) from qiskit.providers.aer.noise.passes import RelaxationNoisePass if self._props_dict is None: self._set_props_dict_from_json() properties = BackendProperties.from_dict(self._props_dict) basis_gates = self.operation_names num_qubits = self.num_qubits dt = self.dt noise_model = NoiseModel(basis_gates=basis_gates) # Add single-qubit readout errors if readout_error: for qubits, error in basic_device_readout_errors(properties): noise_model.add_readout_error(error, qubits) # Add gate errors with warnings.catch_warnings(): warnings.filterwarnings( "ignore", module="qiskit.providers.aer.noise.device.models", ) gate_errors = basic_device_gate_errors( properties, gate_error=gate_error, thermal_relaxation=thermal_relaxation, gate_lengths=gate_lengths, gate_length_units=gate_length_units, temperature=temperature, standard_gates=standard_gates, ) for name, qubits, error in gate_errors: noise_model.add_quantum_error(error, name, qubits) if thermal_relaxation: # Add delay errors via RelaxationNiose pass try: excited_state_populations = [ _excited_population(freq=properties.frequency(q), temperature=temperature) for q in range(num_qubits) ] except BackendPropertyError: excited_state_populations = None try: delay_pass = RelaxationNoisePass( t1s=[properties.t1(q) for q in range(num_qubits)], t2s=[properties.t2(q) for q in range(num_qubits)], dt=dt, op_types=Delay, excited_state_populations=excited_state_populations, ) noise_model._custom_noise_passes.append(delay_pass) except BackendPropertyError: # Device does not have the required T1 or T2 information # in its properties pass return noise_model
def quantum_cadets(n_qubits, noise_circuit, damping_error=0.02): """ n_qubits [int]: total number of qubits - 2^(n_qubits-1) is the number of qft points, plus one sensing qubit noise_circuit [function]: function that takes one input (a time index between 0-1) and returns a quantum circuit with 1 qubit damping_error [float]: T2 damping error """ register_size = n_qubits - 1 # Create a Quantum Circuit acting on the q register qr = QuantumRegister(n_qubits, 'q') cr = ClassicalRegister(register_size) qc = QuantumCircuit(qr, cr) # Add a H gate on qubit 1,2,3...N-1 for i in range(register_size): qc.h(i+1) # multi-qubit controlled-not (mcmt) gate mcmt_gate = MCMT(XGate(), register_size, 1) qr_range=[*range(1, n_qubits), 0] for bit in range(2**register_size): qc.append(mcmt_gate, [qr[i] for i in qr_range]) # external noise gates qc.append(noise_circuit(bit / 2**register_size), [qr[0]]) qc.append(mcmt_gate, [qr[i] for i in qr_range]) if bit == 0: for i in range(register_size): qc.x(i + (n_qubits - register_size)) elif bit == 2**register_size - 1: pass else: conv_register, XRange = build_encode_circuit(bit, n_qubits, register_size) qc.append(conv_register, qr[XRange]) # run the QFT qft = circuit.library.QFT(register_size) qc.append(qft, qr[1:n_qubits]) # map the quantum measurement to classical bits qc.measure(range(1, n_qubits), range(0, register_size)) # display the quantum circuit in text form print(qc.draw('text')) #qc.draw('mpl') plt.show() # noise model t2_noise_model = NoiseModel() t2_noise_model.add_quantum_error(phase_damping_error(damping_error), 'id', [0]) # run the quantum circuit on the statevector simulator backend #backend = Aer.get_backend('statevector_simulator') # run the quantum circuit on the qasm simulator backend backend = Aer.get_backend('qasm_simulator') # number of histogram samples shots = 10000 # execute the quantum program job = execute(qc, backend, noise_model=t2_noise_model, shots=shots) # outputstate = result.get_statevector(qc, decimals=3) # visualization.plot_state_city(outputstate) result = job.result() # collect the state histogram counts counts = result.get_counts(qc) #plot_histogram(counts) qft_result = np.zeros(2**register_size) for f in range(len(qft_result)): # invert qubit order and convert to string f_bin_str = ('{0:0' + str(register_size) + 'b}').format(f)[::-1] if f_bin_str in counts: if f: # flip frequency axis and assign histogram counts qft_result[2**register_size - f] = counts[f_bin_str] / shots else: # assign histogram counts, no flipping because of qft representation (due to nyquist sampling?) qft_result[0] = counts[f_bin_str] / shots freq = np.arange(2**register_size) plt.plot(freq, qft_result, label='QFT') plt.xlabel('Frequency (Hz)') # print the final measurement results print('QFT spectrum:') print(qft_result) # show the plots plt.show()
class NoisyQuantumCircuit(QuantumCircuit): ## def __init__(self, qReg, cReg, nQubits, errors1Qubit, errors2Qubit): ## self.qCircuit = qCircuit(qReg, cReg) ## self.createNoiseModel(nQubits, errors1Qubit, errors2Qubit) def __init__(self, qCircuit, nQubits, errors1Qubit, errors2Qubit): self.qCircuit = qCircuit self.createNoiseModel(nQubits, errors1Qubit, errors2Qubit) def createNoiseModel(self, nQubits, errors1Qubit, errors2Qubit): self.noiseModel = NoiseModel() for i in range(nQubits): for error in errors1Qubit: self.noiseModel.add_quantum_error(error, ['h'], [i]) self.noiseModel.add_quantum_error(error, ['iden'], [i]) self.noiseModel.add_quantum_error(error, ['rx'], [i]) self.noiseModel.add_quantum_error(error, ['ry'], [i]) self.noiseModel.add_quantum_error(error, ['rz'], [i]) self.noiseModel.add_quantum_error(error, ['s'], [i]) self.noiseModel.add_quantum_error(error, ['sdg'], [i]) self.noiseModel.add_quantum_error(error, ['t'], [i]) self.noiseModel.add_quantum_error(error, ['tdg'], [i]) self.noiseModel.add_quantum_error(error, ['u0'], [i]) self.noiseModel.add_quantum_error(error, ['u1'], [i]) self.noiseModel.add_quantum_error(error, ['u2'], [i]) # self.noiseModel.add_quantum_error(error, ['u3'], [i]) self.noiseModel.add_quantum_error(error, ['x'], [i]) self.noiseModel.add_quantum_error(error, ['y'], [i]) self.noiseModel.add_quantum_error(error, ['z'], [i]) for error in errors2Qubit: for j in range(nQubits): self.noiseModel.add_quantum_error(error, ['cx'], [i, j]) self.noiseModel.add_quantum_error(error, ['cy'], [i, j]) self.noiseModel.add_quantum_error(error, ['cz'], [i, j]) self.noiseModel.add_quantum_error(error, ['ch'], [i, j]) self.noiseModel.add_quantum_error(error, ['crz'], [i, j]) self.noiseModel.add_quantum_error(error, ['cu1'], [i, j]) self.noiseModel.add_quantum_error(error, ['cu3'], [i, j]) self.noiseModel.add_quantum_error(error, ['swap'], [i, j]) def getQuantumCircuit(self): return self.qCircuit def getNoiseModel(self): return self.noiseModel
print(noise_model) # ### Specific qubit quantum error # # This applies the error to any occurrence of an instruction acting on a specified list of qubits. Note that the order of the qubit matters: For a 2-qubit gate an error applied to qubits [0, 1] is different to one applied to qubits [1, 0] for example. # # It is added as `noise_model.add_quantum_error(error, instructions, qubits)`: # In[10]: # Create an empty noise model noise_model = NoiseModel() # Add depolarizing error to all single qubit u1, u2, u3 gates on qubit 0 only error = depolarizing_error(0.05, 1) noise_model.add_quantum_error(error, ['u1', 'u2', 'u3'], [0]) # Print noise model info 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()
for t1a, t2a in zip(T1s, T2s) ] for t1b, t2b in zip(T1s, T2s)] errors_swap = [[ noise.thermal_relaxation_error(t1a, t2a, time_swap).expand( noise.thermal_relaxation_error(t1b, t2b, time_swap)) for t1a, t2a in zip(T1s, T2s) ] for t1b, t2b in zip(T1s, T2s)] print(len(errors_cx)) # Add errors to noise model noise_thermal = NoiseModel() #noise_thermal.add_all_qubit_quantum_error(error_1, ['h', 't', 'tdg']) #noise_thermal.add_all_qubit_quantum_error(error_2, ['cx', 'swap']) for j in range(numq): #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_h[j], "h", [j]) noise_thermal.add_quantum_error(errors_t[j], "t", [j]) noise_thermal.add_quantum_error(errors_tdg[j], "tdg", [j]) for k in range(numq): if j != k: noise_thermal.add_quantum_error(errors_cx[j][k], "cx", [j, k]) noise_thermal.add_quantum_error(errors_swap[j][k], "swap", [j, k]) # Get basis gates from noise model basis_gates3 = noise_thermal.basis_gates #load(nisq_sim/best_adder_4_lazy.circ) exec(open(app_name).read()) backend = Aer.get_backend('qasm_simulator')
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(4): 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(4): noise_thermal.add_quantum_error(errors_cx[j][k], "cx", [j, k]) print(noise_thermal) # In[9]: #%% # Run the noisy simulation thermal_simulator = QasmSimulator(noise_model=noise_thermal)
def generateNoiseModel(machine, coherent=True, incoherent=False, readout=False, custom_t=False, t1=None, t2=None, reverse=False): """ Returns a realistic copy of london noise model with custom t1, t2 times """ #initializing noise model noise_thermal = NoiseModel() amp = 1 #for every qubit (5 qubit london machine) for q in range(5): #types of erroneous gates gates = ['u3', 'u2', 'u1', 'id'] for gate in gates: dep_error = None if (coherent): dep_error = generateDepolarizingError(machine, gate, [q]) rel_error = None if (incoherent): generateRelaxationError(machine, gate, [q], t1, t2, amp=amp, custom_t=custom_t) if (dep_error == None and rel_error != None): error_obj = rel_error noise_thermal.add_quantum_error(error_obj, gate, [q]) elif (dep_error != None and rel_error == None): error_obj = dep_error noise_thermal.add_quantum_error(error_obj, gate, [q]) elif (dep_error != None and rel_error != None): error_obj = dep_error.compose(rel_error) noise_thermal.add_quantum_error(error_obj, gate, [q]) #2 qubit gate errors qubits = [i for i in range(5)] qubits.remove(q) for j in qubits: dep_error = None if (coherent): dep_error = generateDepolarizingError(machine, 'cx', [q, j]) rel_error = None if (incoherent): rel_error = generateRelaxationError(machine, 'cx', [q, j], t1, t2, amp=amp, custom_t=custom_t) if (dep_error == None and rel_error != None): error_obj = rel_error noise_thermal.add_quantum_error(error_obj, 'cx', [q, j]) elif (dep_error != None and rel_error == None): error_obj = dep_error noise_thermal.add_quantum_error(error_obj, 'cx', [q, j]) elif (dep_error != None and rel_error != None): error_obj = dep_error.compose(rel_error) noise_thermal.add_quantum_error(error_obj, 'cx', [q, j]) if (readout): #adding the readout error p1_0 = machine.properties().qubit_property(q, 'prob_meas1_prep0')[0] p0_1 = machine.properties().qubit_property(q, 'prob_meas0_prep1')[0] print('Original: ' + str(p1_0) + ' ' + str(p0_1)) if (reverse): temp = p1_0 p1_0 = p0_1 p0_1 = temp print('Reverse: ' + str(p1_0) + ' ' + str(p0_1)) matrix = [[1 - p1_0, p1_0], [p0_1, 1 - p0_1]] error = ReadoutError(matrix) noise_thermal.add_readout_error(error, [q]) return noise_thermal