def simulate_samples(self, repetitions: int) -> study.TrialResult: """Samples the circuit Args: repetitions: Number of times the circuit is simulated Returns: TrialResult from Cirq.Simulator """ if self.simulate_ideal: noise_model = devices.NO_NOISE else: noise_model = AQTNoiseModel() if self.circuit == Circuit(): raise RuntimeError('simulate ideal called without a valid circuit') sim = DensityMatrixSimulator(noise=noise_model) result = sim.run(self.circuit, repetitions=repetitions) return result
def _circuit_to_choi(circuit: Circuit) -> np.ndarray: """Returns the density matrix of the Choi state associated to the input circuit. The density matrix completely characterizes the quantum channel induced by the input circuit (including the effect of noise if present). Args: circuit: The input circuit. Returns: The density matrix of the Choi state associated to the input circuit. """ simulator = DensityMatrixSimulator() num_qubits = len(circuit.all_qubits()) # Copy and remove all operations full_circ = deepcopy(circuit)[0:0] full_circ += _max_ent_state_circuit(2 * num_qubits) full_circ += circuit return simulator.simulate(full_circ).final_density_matrix # type: ignore
cooling_out_exists = False if os.path.exists(outfile_reheating): print(f'output file {outfile_reheating} exists already.') reheating_out_exists = True else: reheating_out_exists = False if cooling_out_exists and reheating_out_exists: print('exiting.') exit() print('\nBuilding circuit') stopwatch = time.time() system = spinmodels.TFIMChain(L, JvB, 1) system.normalize() circuit = qdccirq.logsweep_protocol(system, K) simulator = DensityMatrixSimulator() print('done in', time.time() - stopwatch, 'seconds.') # cooling if not cooling_out_exists: print('\nRunning cooling simulation') stopwatch = time.time() # build maximally-mixed system state tensored with |0><0| fridge init_state = np.diag( np.tile(np.array([1 / 2**L, 0], dtype=np.complex64), 2**L)) res = simulator.simulate( circuit, initial_state=init_state, qubit_order=[*system.get_qubits(), qdccirq.FRIDGE])
"""This module contains methods for benchmarking mitiq error extrapolation against a standard QAOA for MAXCUT. """ from typing import Callable, List, Optional, Tuple import numpy as np from cirq import Circuit, NamedQubit, X, ZZ, H, DensityMatrixSimulator from cirq import identity_each as id from scipy.optimize import minimize from mitiq import execute_with_zne, QPROGRAM from mitiq.zne.inference import Factory from mitiq.benchmarks.utils import noisy_simulation SIMULATOR = DensityMatrixSimulator() def make_noisy_backend( noise: float, obs: np.ndarray ) -> Callable[[Circuit], float]: """Helper function to match mitiq's backend type signature. Args: noise: The level of depolarizing noise. obs: The observable that the backend should measure. Returns: A mitiq backend function. """