def custom_noise_configuration(remote_backend=_REMOTE_BACKEND_NAME): logger.info('Produce custom noise configuration') logger.info('Loading IBM account...') before = datetime.now() provider = IBMQ.load_account() delta = datetime.now() - before logger.info('IBM account loaded ({} s)'.format(delta.total_seconds())) device = provider.get_backend(remote_backend) coupling_map = device.configuration().coupling_map # QuantumError objects error_reset = pauli_error([('X', _P_RESET), ('I', 1 - _P_RESET)]) error_meas = pauli_error([('X', _P_MEAS), ('I', 1 - _P_MEAS)]) error_gate1 = pauli_error([('X', _P_GATE1), ('I', 1 - _P_GATE1)]) error_gate2 = error_gate1.tensor(error_gate1) # Add errors to noise model noise_bit_flip = NoiseModel() noise_bit_flip.add_all_qubit_quantum_error(error_reset, "reset") noise_bit_flip.add_all_qubit_quantum_error(error_meas, "measure") noise_bit_flip.add_all_qubit_quantum_error(error_gate1, ["u1", "u2", "u3"]) noise_bit_flip.add_all_qubit_quantum_error(error_gate2, ["cx"]) return (noise_bit_flip, coupling_map)
def calculateEnergy(self, transpiledCircuit): if self.useNoiseModel == True: p_reset = 0.003 p_meas = 0.01 p_gate1 = 0.05 # QuantumError objects error_reset = pauli_error([('X', p_reset), ('I', 1 - p_reset)]) error_meas = pauli_error([('X', p_meas), ('I', 1 - p_meas)]) error_gate1 = pauli_error([('X', p_gate1), ('I', 1 - p_gate1)]) error_gate2 = error_gate1.tensor(error_gate1) noise_bit_flip = NoiseModel() noise_bit_flip.add_all_qubit_quantum_error(error_reset, "reset") noise_bit_flip.add_all_qubit_quantum_error(error_meas, "measure") noise_bit_flip.add_all_qubit_quantum_error(error_gate1, ["u1", "u2", "u3"]) noise_bit_flip.add_all_qubit_quantum_error(error_gate2, ["cx"]) simulator = QasmSimulator() job = execute(transpiledCircuit, backend=simulator, basis_gates=noise_bit_flip.basis_gates, noise_model=noise_bit_flip) results = job.result() counts = results.get_counts() _paulis, sgift = get_pauliList(self.ising, self.qubits) _basis = [(pauli[1], [i]) for i, pauli in enumerate(_paulis)] num_shots = sum(list(results.get_counts().values())) results = parallel_map(WeightedPauliOperator._routine_compute_mean_and_var, [([_paulis[idx] for idx in indices], results.get_counts()) for basis, indices in _basis], num_processes=aqua_globals.num_processes) return results[0][0] else: _paulis, sgift = get_pauliList(self.ising, self.qubits) _basis = [(pauli[1], [i]) for i, pauli in enumerate(_paulis)] job = execute(transpiledCircuit, backend=self.backend) results = job.result() counts = results.get_counts() num_shots = sum(list(results.get_counts().values())) results = parallel_map(WeightedPauliOperator._routine_compute_mean_and_var, [([_paulis[idx] for idx in indices], results.get_counts()) for basis, indices in _basis], num_processes=aqua_globals.num_processes) return results[0][0]
def test_process_characterisation_complete_noise_model() -> None: my_noise_model = NoiseModel() readout_error_0 = 0.2 readout_error_1 = 0.3 my_noise_model.add_readout_error( [ [1 - readout_error_0, readout_error_0], [readout_error_0, 1 - readout_error_0], ], [0], ) my_noise_model.add_readout_error( [ [1 - readout_error_1, readout_error_1], [readout_error_1, 1 - readout_error_1], ], [1], ) my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 1]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [0]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Z", 0.65)]), ["u2"], [0]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Y", 0.65)]), ["u1"], [0]) back = AerBackend(my_noise_model) char = cast(Dict[str, Any], back.characterisation) dev = Device( char.get("NodeErrors", {}), char.get("EdgeErrors", {}), char.get("Architecture", Architecture([])), ) assert char["GenericTwoQubitQErrors"][(0, 1)][0][1][0] == 0.0375 assert char["GenericTwoQubitQErrors"][(0, 1)][0][1][15] == 0.4375 assert char["GenericOneQubitQErrors"][0][0][1][0] == 0.125 assert char["GenericOneQubitQErrors"][0][0][1][3] == 0.625 assert char["GenericOneQubitQErrors"][0][1][1][0] == 0.35 assert char["GenericOneQubitQErrors"][0][1][1][1] == 0.65 assert char["GenericOneQubitQErrors"][0][2][1][0] == 0.35 assert char["GenericOneQubitQErrors"][0][2][1][1] == 0.65 assert dev.get_error(OpType.U3, dev.nodes[0]) == 0.375 assert dev.get_error(OpType.CX, (dev.nodes[0], dev.nodes[1])) == 0.5625 assert dev.get_error(OpType.CX, (dev.nodes[1], dev.nodes[0])) == 0.80859375 assert char["ReadoutErrors"][0] == [[0.8, 0.2], [0.2, 0.8]] assert char["ReadoutErrors"][1] == [[0.7, 0.3], [0.3, 0.7]]
def get_noise(noisy): """Returns a noise model when input is not False or None. A string will be interpreted as the name of a backend, and the noise model of this will be extracted. A float will be interpreted as an error probability for a depolarizing+measurement error model. Anything else (such as True) will give the depolarizing+measurement error model with default error probabilities.""" if noisy: if type(noisy) is str: # get noise information from a real device (via the IBM Q Experience) device = get_backend(noisy) noise_model = noise.device.basic_device_noise_model( device.properties() ) else: # make a simple noise model for a given noise strength if type(noisy) is float: p_meas = noisy p_gate1 = noisy else: # default values p_meas = 0.08 p_gate1 = 0.04 error_meas = pauli_error([('X',p_meas), ('I', 1 - p_meas)]) # bit flip error with prob p_meas error_gate1 = depolarizing_error(p_gate1, 1) # replaces qubit state with nonsense with prob p_gate1 error_gate2 = error_gate1.kron(error_gate1) # as above, but independently on two qubits noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") # add bit flip noise to measurement noise_model.add_all_qubit_quantum_error(error_gate1, ["u1", "u2", "u3"]) # add depolarising to single qubit gates noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) # add two qubit depolarising to two qubit gates else: noise_model = None return noise_model
def test_backend_method_clifford_circuits_and_pauli_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=True) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) qobj = compile(circuits, self.SIMULATOR, shots=shots) def get_result(): return self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS, noise_model=noise_model).result() # Check simulation method method = self.BACKEND_OPTS.get('method') result = self.SIMULATOR.run( qobj, backend_options=self.BACKEND_OPTS).result() self.is_completed(result) if method == 'statevector': self.compare_result_metadata(result, circuits, 'method', 'statevector') else: self.compare_result_metadata(result, circuits, 'method', 'stabilizer')
def get_noise_model(p_err): error_gate1 = pauli_error([("X", p_err / 2), ("Z", p_err / 2), ("I", 1 - p_err)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_gate1, "id") return noise_model
def test_backend_method_nonclifford_circuit_and_unitary_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=False) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) # Test circuits shots = 100 circuits = ref_non_clifford.ccx_gate_circuits_deterministic( final_measure=True) qobj = assemble(circuits, self.SIMULATOR, shots=shots) result = self.SIMULATOR.run(qobj, backend_options=self.BACKEND_OPTS, noise_model=noise_model).result() success = getattr(result, 'success', False) # Check simulation method method = self.BACKEND_OPTS.get('method', 'automatic') if method == 'stabilizer': self.assertFalse(success) else: self.assertTrue(success) if method == 'automatic': target_method = 'density_matrix' else: target_method = method self.compare_result_metadata(result, circuits, 'method', target_method)
def get_noise(p_meas, p_gate): error_meas = pauli_error([('X', p_meas), ('I', 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") noise_model.add_all_qubit_quantum_error(error_gate1, ["x"]) noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) return noise_model
def get_noise(p_meas, p_gate): """Define a noise model.""" error_meas = pauli_error([("X", p_meas), ("I", 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") noise_model.add_all_qubit_quantum_error(error_gate1, ["u1", "u2", "u3"]) noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) return noise_model
def get_noise(p_meas,p_gate): error_meas = pauli_error([('X',p_meas), ('I', 1 - p_meas)]) error_gate1 = depolarizing_error(p_gate, 1) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") # measurement error is applied to measurements noise_model.add_all_qubit_quantum_error(error_gate1, ["x"]) # single qubit gate error is applied to x gates noise_model.add_all_qubit_quantum_error(error_gate2, ["cx"]) # two qubit gate error is applied to cx gates return noise_model
def test_process_characterisation_incomplete_noise_model() -> None: my_noise_model = NoiseModel() my_noise_model.add_quantum_error(depolarizing_error(0.6, 2), ["cx"], [0, 1]) my_noise_model.add_quantum_error(depolarizing_error(0.5, 1), ["u3"], [1]) my_noise_model.add_quantum_error(depolarizing_error(0.1, 1), ["u3"], [3]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Z", 0.65)]), ["u2"], [0]) my_noise_model.add_quantum_error(pauli_error([("X", 0.35), ("Y", 0.65)]), ["u1"], [2]) back = AerBackend(my_noise_model) char = cast(Dict[str, Any], back.characterisation) c = Circuit(4).CX(0, 1).H(2).CX(2, 1).H(3).CX(0, 3).H(1).X(0).measure_all() back.compile_circuit(c) assert back.valid_circuit(c) dev = Device( char.get("NodeErrors", {}), char.get("EdgeErrors", {}), char.get("Architecture", Architecture([])), ) nodes = dev.nodes assert set(dev.architecture.coupling) == set([ (nodes[0], nodes[1]), (nodes[0], nodes[2]), (nodes[0], nodes[3]), (nodes[1], nodes[2]), (nodes[1], nodes[3]), (nodes[2], nodes[0]), (nodes[2], nodes[1]), (nodes[2], nodes[3]), (nodes[3], nodes[0]), (nodes[3], nodes[1]), (nodes[3], nodes[2]), ])
def get_noise_model(p_err): error_gate1 = pauli_error([("X", p_err / 2), ("Z", p_err / 2), ("I", 1 - p_err)]) error_gate2 = error_gate1.tensor(error_gate1) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error( error_gate1, "measure") # measurement error is applied to measurements noise_model.add_all_qubit_quantum_error( error_gate1, ["u1", "u2", "u3"]) # single qubit gate error is applied to x gates noise_model.add_all_qubit_quantum_error( error_gate2, ["cx"]) # two qubit gate error is applied to cx gates return noise_model
def test_auto_method_clifford_circuits_and_unitary_noise(self): """Test statevector method is used for Clifford circuit""" # Noise Model error = pauli_error([['XX', 0.5], ['II', 0.5]], standard_gates=False) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error, ['cz', 'cx']) backend = self.backend(noise_model=noise_model) # Test circuits shots = 100 circuits = ref_2q_clifford.cz_gate_circuits_deterministic( final_measure=True) result = backend.run(circuits, shots=shots).result() success = getattr(result, 'success', False) self.compare_result_metadata(result, circuits, 'method', "density_matrix")
def get_noise(p): error_meas = pauli_error([('X',p), ('I', 1 - p)]) noise_model = NoiseModel() noise_model.add_all_qubit_quantum_error(error_meas, "measure") # measurement error is applied to measurements return noise_model
# # `QuantumError` instances can be combined by using composition, tensor product, and tensor expansion (reversed order tensor product) to produce new `QuantumErrors` as: # # * Composition: $\cal{E}(\rho)=\cal{E_2}(\cal{E_1}(\rho))$ as `error = error1.compose(error2)` # * Tensor product: $\cal{E}(\rho) =(\cal{E_1}\otimes\cal{E_2})(\rho)$ as `error error1.tensor(error2)` # * Expand product: $\cal{E}(\rho) =(\cal{E_2}\otimes\cal{E_1})(\rho)$ as `error error1.expand(error2)` # ### Example # # For example to construct a 5% single-qubit Bit-flip error: # In[2]: # Construct a 1-qubit bit-flip and phase-flip errors p_error = 0.05 bit_flip = pauli_error([('X', p_error), ('I', 1 - p_error)]) phase_flip = pauli_error([('Z', p_error), ('I', 1 - p_error)]) print(bit_flip) print(phase_flip) # In[3]: # Compose two bit-flip and phase-flip errors bitphase_flip = bit_flip.compose(phase_flip) print(bitphase_flip) # In[4]: # Tensor product two bit-flip and phase-flip errors with # bit-flip on qubit-0, phase-flip on qubit-1 error2 = phase_flip.tensor(bit_flip)