def simulate(sim_type: str, num_qubits: int, num_gates: int, num_prefix_qubits: int = 0, use_processes: bool = False) -> None: """"Runs the simulator.""" circuit = cirq.Circuit() for _ in range(num_gates): which = np.random.choice(['expz', 'expw', 'exp11']) if which == 'expw': circuit.append(ExpWGate(phase_exponent=np.random.random(), exponent=np.random.random()).on( np.random.randint(num_qubits)), strategy=cirq.InsertStrategy.EARLIEST) elif which == 'expz': circuit.append(cirq.Z( np.random.randint(num_qubits))**np.random.random(), strategy=cirq.InsertStrategy.EARLIEST) elif which == 'exp11': q1, q2 = np.random.choice(num_qubits, 2, replace=False) circuit.append(cirq.CZ(q1, q2)**np.random.random(), strategy=cirq.InsertStrategy.EARLIEST) if sim_type == _XMON: XmonSimulator( XmonOptions(num_shards=2**num_prefix_qubits, use_processes=use_processes)).run(circuit) elif sim_type == _UNITARY: circuit.apply_unitary_effect_to_state(initial_state=0)
def run(simulator: cg.XmonSimulator, circuit: cirq.Circuit, scheduler: Optional[Callable], **kw): if scheduler is None: program = circuit else: program = scheduler(cirq.UnconstrainedDevice, circuit) return simulator.run(program, **kw)
def _run_sim_stage3(aht, ht, zz, exact=False, print_circuit=False, noisy=False): """Helper routine to executes state preparation circuit a single time. Outputs a state vector. Args: ===== aht, ht, zz : numeric Parameters for decoding circuit exact : bool If True, works with wavefunction print_circuit : bool If True, prints circuit noisy : bool If True, runs noisy version of circuit Returns: ======== final_state : numpy.ndarray Final state vector """ exact = True # NOTE: Hard-coded for now simulator = XmonSimulator() if noisy: circuit_run, resolvers = noisy_job_stage3(aht, ht, zz, exact) for resolver in resolvers: result = simulator.simulate(circuit=circuit_run, param_resolver=resolver, qubit_order=qubit_ordering) else: circuit_run = decoder_circuit(aht, ht, zz, exact) result = simulator.simulate(circuit=circuit_run, qubit_order=qubit_ordering) if print_circuit: print(circuit_run.to_text_diagram(use_unicode_characters=False)) return result.final_state
def make_ansatz(self, theta_list=None, simulator=XmonSimulator()): if theta_list is None: #theta_list = self.theta_list theta_list = self.make_param() else: theta_list = theta_list circuit = self.first_cycle_circuit(theta_list, self.n_qubit) result = simulator.simulate(circuit) psi = result.final_state.reshape(len(result.final_state), 1) return psi
def circuit_init(meas=True): if meas: yield XmonMeasurementGate(key='q0')(q[0]) yield XmonMeasurementGate(key='q1')(q[1]) yield XmonMeasurementGate(key='q2')(q[2]) yield XmonMeasurementGate(key='q3')(q[3]) circuit.append(circuit_init()) print(" ") print(" ") print(circuit) print(" ") print(" ") simulator = XmonSimulator() result = simulator.run(circuit) measured = int(str(result)[3]) print(str(result)) print(str(result)[3]) print(str(result)[8]) print(str(result)[13]) if ((str(result)[3] + str(result)[8] + str(result)[13] + str(result)[18]) == "0000"): graphing[0] = graphing[0] + 1 if ((str(result)[3] + str(result)[8] + str(result)[13] + str(result)[18]) == "0001"):
def _run_sim_stage2(a, b, x, z, alpha, exact=False, print_circuit=False, noisy=False): """Executes circuit a single time. Outputs 1 for a success (i.e. reference qubits are |000>) and 0 for a failure. Args: ===== a, b, x, z : numeric Circuit parameters for encoding circuit alpha : numeric Parameter for state preparation circuit exact : bool If True, works with wavefunction print_circuit : bool If True, prints circuit noisy : bool If True, runs noisy version of circuit Returns: ======== total : int Value of 1 if reference qubits are all 0's. Value of 0 else. """ simulator = XmonSimulator() if noisy: circuit_run, resolvers = noisy_job(a, b, x, z, alpha, exact) else: circuit_run = compression_circuit(a, b, x, z, alpha, exact) if exact: if noisy: for resolver in resolvers: result = simulator.simulate(circuit=circuit_run, param_resolver=resolver) else: result = simulator.simulate(circuit=circuit_run) avg = 0 for j in range(2): avg += np.abs(result.final_state[j])**2 return avg else: if noisy: for resolver in resolvers: result = simulator.run(circuit=circuit_run, param_resolver=resolver, repetitions=1) else: result = simulator.run(circuit=circuit_run, repetitions=1) reference_measurements = [] reference_labels = ['r00', 'r01', 'r10'] for j in reference_labels: reference_measurements.append(int(result.measurements[j][0])) total = 0 res = [] for y in range(3): res.append(reference_measurements[y]) if res == [0, 0, 0]: total = 1 if print_circuit == True: print(circuit_run.to_text_diagram(use_unicode_characters=False)) return total
def apply_h(): yield cirq.CCX.on(target[0], target[1], final) yield cirq.CCX.on(target[0], target[2], final) yield cirq.CCX.on(target[1], target[2], final) yield cirq.CNOT.on(target[0], final) yield cirq.CNOT.on(target[1], final) yield cirq.CNOT.on(target[2], final) #yield cirq.H.on(final) circuit.append(apply_h()) def circuit_init_again(meas=True): if meas: yield XmonMeasurementGate(key='qubit')(final) #circuit.append() circuit.append(circuit_init_again()) print(" ") print(" ") print(circuit) print(" ") print(" ") simulator = XmonSimulator() result = simulator.run(circuit, repetitions=20) print(result)
for order, steps in product((1, 2), Steps): circuit = cirq.Circuit() init = [] for i in range(sites // 2 + sites % 2): init.append( xmon_gates.ExpWGate(half_turns=1.0, axis_half_turns=0.0)(qubits[i])) for i in range(sites // 2, sites): init.append( xmon_gates.ExpWGate(half_turns=1.0, axis_half_turns=0.0)(qubits[i + sites])) circuit.append(init, strategy=cirq.circuits.InsertStrategy.EARLIEST) for j in range(steps): circuit.append(trotter_step(t / steps, U / steps, qubits, order=order), strategy=cirq.circuits.InsertStrategy.EARLIEST) simulator = XmonSimulator() result = simulator.simulate(circuit) h = np.sum([ FermionOperator(((i, 1), (i + 1, 0)), t) + FermionOperator( ((i + 1, 1), (i, 0)), t) + FermionOperator( ((i + sites, 1), (i + 1 + sites, 0)), t) + FermionOperator( ((i + 1 + sites, 1), (i + sites, 0)), t) for i in range(sites - 1) ]) h += FermionOperator(((sites // 2, 1), (sites // 2, 0), (sites // 2 + sites, 1), (sites // 2 + sites, 0)), U) init = FermionOperator( tuple([(i, 1) for i in range(sites // 2 + sites % 2)] + [(i + sites, 1) for i in range(sites // 2, sites)]), 1.0) init = get_sparse_operator(init, sites * 2)