def compute_players(self): self.measure_players() simulator = Simulator() for player in self.players: result = '' if player.next_qubit1 != 0 or player.next_qubit2 != 0: result = simulator.run(player.circuit) res = str(result) bits1 = '' bits2 = '' bit = '' for i in range(len(res)): if res[i] == '=': bit = bit + str(res[i + 1]) if player.next_qubit1 > 0: bits1 = bit[:player.next_qubit1] if player.next_qubit2 > 0: bits2 = bit[player.next_qubit1:] if len(player.card1) > 1: player.card1 = [player.card1.pop(int(bits1, 2))] if len(player.card2) > 1: player.card2 = [player.card2.pop(int(bits2, 2))]
def find_y(N, a, q, reps=5 ): # Uses the order finding algorithm to give possible values for y simulator = Simulator() # Sets up simulator circuit = cirq.Circuit() number_qubits = q # Sets up all qubits to be used. m_qubits = [cirq.GridQubit(i, 0) for i in range(2 * number_qubits)] x_qubits = [ cirq.GridQubit(i, 0) for i in range(2 * number_qubits, 3 * number_qubits) ] b_qubits = [ cirq.GridQubit(i, 0) for i in range(3 * number_qubits, 4 * number_qubits + 1) ] zero_qubit = cirq.GridQubit(4 * number_qubits + 1, 0) print('Number of qubits: {}'.format(4 * number_qubits + 5)) circuit.append(order_find(a, N, b_qubits, x_qubits, zero_qubit, m_qubits), strategy=InsertStrategy.NEW) # Runs the simulation. return simulator.run(circuit, repetitions=reps).histogram( key='q') # Returns a dictionary of the measurment results.
sqrt_x = cirq.X**0.5 yield sqrt_x(q0), sqrt_x(q1) yield cirq.CZ(q0, q1) yield sqrt_x(q0), sqrt_x(q1) if meas: yield cirq.measure(q0, key='q0'), cirq.measure(q1, key='q1') circuit = cirq.Circuit() circuit.append(basic_circuit()) print(circuit) from cirq import Simulator simq = Simulator() result = simq.run(circuit) print(result) #Ciruit Stepping circuit = cirq.Circuit() circuit.append(basic_circuit()) for i, step in enumerate(simq.simulate_moment_steps(circuit)): print('state at step %d: %s' % (i, np.around(step.state_vector(), 3))) #monte carlo noise q = cirq.NamedQubit('a') circuit = cirq.Circuit.from_ops(cirq.bit_flip(p=0.2)(q), cirq.measure(q)) #PauliXGate simq = cirq.Simulator()
q1 = cirq.GridQubit(1, 0) def basic_circuit(meas=True): sqrt_x = cirq.X**0.5 yield sqrt_x(q0), sqrt_x(q1) yield cirq.CZ(q0, q1) yield sqrt_x(q0), sqrt_x(q1) if meas: yield cirq.measure(q0, key='q0'), cirq.measure(q1, key='q1') circuit = cirq.Circuit() circuit.append(basic_circuit()) print(circuit) simulator = Simulator() result = simulator.run(circuit) print(result) # Circuit : # (0, 0): ───X^0.5───@───X^0.5───M('q0')─── # │ # (1, 0): ───X^0.5───@───X^0.5───M('q1')─── # Result # q0=0 # q1=1
for _ in range(number_of_iterations): circuit.append(oracle) circuit.append(self.diffusion) return circuit def make_oracle(qubits): circuit = cirq.Circuit() controllee= [] for i in range(length - 1): controllee.append(qubits[i]) circuit.append(cirq.X(qubits[i]) for i in [1,3,4]) circuit.append(cirq.control(cirq.Z, controllee)(qubits[-1])) circuit.append(cirq.X(qubits[i]) for i in [1,3,4]) return circuit grover = Grover(qubits) oracle = make_oracle(qubits) circuit = cirq.Circuit() circuit.append(cirq.H(q) for q in qubits) grover.run(circuit, oracle, number_of_expected_results) circuit.append(cirq.measure(*qubits)) print(circuit) from cirq import Simulator simulator = Simulator() result = simulator.run(circuit, repetitions=int(sys.argv[2])) print(result)
def basic_circuit(m=True): sqrt_x = cirq.X**0.5 yield sqrt_x(q0), sqrt_x(q1) yield cirq.CZ(q0, q1) yield sqrt_x(q0), sqrt_x(q1) if (m): yield cirq.measure(q0, key='alpha'), cirq.measure(q1, key='beta') circuit = cirq.Circuit() circuit.append(basic_circuit()) print(circuit) # run the simulator sim = Simulator() res = sim.run(circuit) print(res) def main(): qft_circuit = generate_2x2_grid() print("circuit:") print(qft_circuit) sim = Simulator() res = sim.simulate(qft_circuit) print('\nfinal state:') print(np.around(res.final_state, 3)) def cz_swap(q0, q1, rot): yield cirq.CZ(q0, q1)**rot
n = 4 length = n + 1 num_reps = 200 a, subset = choose_subset(n) circuit = cirq.Circuit() b = np.random.randint(0, high=2, size=n) l_z = subset_parity(subset, b, n) print(a, b) qubits = [cirq.GridQubit(i, 0) for i in range(length - 1)] readout = cirq.GridQubit(length - 1, 0) circuit.append([prepare_initial_state(b, qubits, n)]) circuit.append([cirq.Rx(-np.pi / 2)(readout)]) for i in range(n): cX = cirq.ControlledGate(cirq.Rx(-np.pi * a[i])) circuit.append([cX(qubits[i], readout)]) circuit.append([measure_Y(readout), cirq.measure(readout, key='demo')]) print(circuit) results = simulator.run(circuit, repetitions=num_reps) estimate = compute_est(results.histogram, num_reps, 'demo') print("computed Value of function", estimate)