def run(angles, num_shots): # create the qubits to be used in the circuit a = cirq.NamedQubit("a") b = cirq.NamedQubit("b") c = cirq.NamedQubit("c") # store the gates to be used when creating the circuit x = cirq.X CRy = [] NegCRy = [] # need to store a controlled Ry gate for each angle, as well as a negative angle one for ang in angles: CRy.append(cirq.ControlledGate(RotYGate(rads=ang))) NegCRy.append(cirq.ControlledGate(RotYGate(rads=-ang))) # create the circuit and add H operations to it circuit = cirq.Circuit() circuit.append(H.on(a)) circuit.append(H.on(b)) # for every angle, add to the circuit the encoding of that angle for i in range(len(angles)): # to make sure we are transforming the correct vector, need to NOT certain qubits circuit.append(x.on(a)) if(i%2 == 0): circuit.append(x.on(b)) # The C^2-Ry operation circuit.append(CRy[i].on(a, c)) circuit.append(CNOT.on(a, b)) circuit.append(NegCRy[i].on(b, c)) circuit.append(CNOT.on(a, b)) circuit.append(CRy[i].on(b, c)) # measure all of the qubits circuit.append(cirq.measure(a)) circuit.append(cirq.measure(b)) circuit.append(cirq.measure(c)) simulator = cirq.google.XmonSimulator() # run the circuit and get measurements trials = simulator.run(circuit, repetitions=num_shots) # use the measurements to recover the angles encoding the colors recovered_angles = probs(trials, num_shots) return recovered_angles
def test_composite_default(): q0, q1 = QubitId(), QubitId() cnot = CNOT(q0, q1) circuit = cirq.Circuit() circuit.append(cnot) opt = cirq.ExpandComposite() opt.optimize_circuit(circuit) expected = cirq.Circuit() expected.append([Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5]) assert_equal_mod_empty(expected, circuit)
def test_multiple_composite_default(): q0, q1 = QubitId(), QubitId() cnot = CNOT(q0, q1) circuit = Circuit() circuit.append([cnot, cnot]) opt = ExpandComposite() opt.optimize_circuit(circuit) expected = Circuit() decomp = [Y(q1)**-0.5, CZ(q0, q1), Y(q1)**0.5] expected.append([decomp, decomp]) assert_equal_mod_empty(expected, circuit)
def test_composite_extension_overrides(): q0, q1 = QubitId(), QubitId() cnot = CNOT(q0, q1) circuit = cirq.Circuit() circuit.append(cnot) ext = cirq.Extensions() ext.add_cast(cirq.CompositeGate, cirq.CNotGate, lambda _: OtherCNot()) opt = cirq.ExpandComposite(composite_gate_extension=ext) opt.optimize_circuit(circuit) expected = cirq.Circuit() expected.append([Z(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, Z(q0)]) assert_equal_mod_empty(expected, circuit)
def test_mix_composite_non_composite(): q0, q1 = QubitId(), QubitId() actual = cirq.Circuit.from_ops(X(q0), CNOT(q0, q1), X(q1)) opt = cirq.ExpandComposite() opt.optimize_circuit(actual) expected = cirq.Circuit.from_ops(X(q0), Y(q1) ** -0.5, CZ(q0, q1), Y(q1) ** 0.5, X(q1), strategy=cirq.InsertStrategy.NEW) assert_equal_mod_empty(expected, actual)
def test_circuit_from_quil(): q0, q1, q2 = LineQubit.range(3) cirq_circuit = Circuit([ I(q0), I(q1), I(q2), X(q0), Y(q1), Z(q2), H(q0), S(q1), T(q2), Z(q0)**(1 / 8), Z(q1)**(1 / 8), Z(q2)**(1 / 8), rx(np.pi / 2)(q0), ry(np.pi / 2)(q1), rz(np.pi / 2)(q2), CZ(q0, q1), CNOT(q1, q2), cphase(np.pi / 2)(q0, q1), cphase00(np.pi / 2)(q1, q2), cphase01(np.pi / 2)(q0, q1), cphase10(np.pi / 2)(q1, q2), ISWAP(q0, q1), pswap(np.pi / 2)(q1, q2), SWAP(q0, q1), xy(np.pi / 2)(q1, q2), CCNOT(q0, q1, q2), CSWAP(q0, q1, q2), MeasurementGate(1, key="ro[0]")(q0), MeasurementGate(1, key="ro[1]")(q1), MeasurementGate(1, key="ro[2]")(q2), ]) # build the same Circuit, using Quil quil_circuit = circuit_from_quil(QUIL_PROGRAM) # test Circuit equivalence assert cirq_circuit == quil_circuit pyquil_circuit = Program(QUIL_PROGRAM) # drop declare and measures, get Program unitary pyquil_unitary = program_unitary(pyquil_circuit[1:-3], n_qubits=3) # fix qubit order convention cirq_circuit_swapped = Circuit(SWAP(q0, q2), cirq_circuit[:-1], SWAP(q0, q2)) # get Circuit unitary cirq_unitary = cirq_circuit_swapped.unitary() # test unitary equivalence assert np.isclose(pyquil_unitary, cirq_unitary).all()
import cirq from cirq.ops import H, T, CNOT, measure from cirq.circuits import InsertStrategy q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)] circuit = cirq.Circuit() circuit.append([H(q0)], strategy=InsertStrategy.NEW) circuit.append([H(q1)], strategy=InsertStrategy.NEW) circuit.append([H(q2)], strategy=InsertStrategy.NEW) circuit.append([T(q0)], strategy=InsertStrategy.NEW) circuit.append([T(q1)], strategy=InsertStrategy.NEW) circuit.append([T(q2)], strategy=InsertStrategy.NEW) circuit.append([CNOT(q0, q1)], strategy=InsertStrategy.NEW) circuit.append([measure(q0)], strategy=InsertStrategy.NEW) circuit.append([measure(q1)], strategy=InsertStrategy.NEW) circuit.append([measure(q2)], strategy=InsertStrategy.NEW) print(circuit)
import cirq from cirq.ops import CNOT from cirq.devices import GridQubit q0, q1 = (GridQubit(0,0), GridQubit(0, 1)) print(CNOT.on(q0, q1)) print(CNOT(q0, q1)) #is it unitary print(cirq.unitary(cirq.X)) #the square root of an X(NOT) gate, true or false sqrt_x = cirq.X**0.5 print(cirq.unitary(sqrt_x)) #the xmon gates
qubits = [cirq.GridQubit(0, i) for i in range(5)] q2 = qubits[2] circuit = cirq.Circuit() circuit.append(H(q2)) if humanMove == "s": circuit.append(S(q2)) else: circuit.append(cirq.inverse(S(q2))) circuit.append([ H(qubits[opponent]), S(q2), H(q2), CNOT(qubits[opponent], q2), H(q2), H(q2), measure(q2, key="m") ]) print(circuit) simulator = cirq.Simulator() results = simulator.run(circuit, repetitions=1) # print(results.histogram(key="m")) keys = results.histogram(key="m").keys() for key in keys: if key == 1: print("You win!")
def run_circuit(player, light1, light0): results_dict = {} # define qubits for circuit q0, q1, q2 = [cirq.GridQubit(i, 0) for i in range(3)] # define quantum circuit circuit = cirq.Circuit() # define quantuum simulator simulator = cirq.Simulator() # if any of the values are one add an X gate at beginning of circuit for that bit if player == 1: circuit.append(X(q2)) if light1 == 1: circuit.append(X(q1)) if light0 == 1: circuit.append(X(q0)) # main circuit construction # H ->Hadamard gate # CNOT -> Feynman gate # X -> Pauli X gate (inverter) # CCX -> CCNOT gate circuit.append(H(q0)) circuit.append(CNOT(q2, q1)) circuit.append(X(q1)) circuit.append(H(q2)) circuit.append(CNOT(q2, q0)) circuit.append(CCX(q2, q0, q1)) circuit.append(X(q1)) circuit.append(cirq.measure(q0, key='x')) circuit.append(cirq.measure(q1, key='y')) # get results from 1000 runs of circuit results = simulator.run(circuit, repetitions=1000) # gets counts for each possible output counts = results.multi_measurement_histogram(keys=['y', 'x']) # place count values from circuit simlation into dictionary results_dict = counter_to_dict(counts) # obtain 1 letter value that corresponds to 1 selected output value out of 1000 possiblities letter_choice = parse_counts(results_dict) # translate the letter value into a mood value choice = determine_state(letter_choice) # print statements that can be set with boolean if print_circuit == True: print(circuit) if print_counts == True: print(counts) if print_stats == True: get_stats() if print_mood == True: print("Robot Mood = " + str(choice)) # return the choice of the robot return choice