def buildCirqCircuit(self, qubits, circuit): cirqCircuit = cirq.Circuit() ## Instantiate a CNot Gate cNotGate = cirq.CNotGate() ## Instantiate a Swap Gate swapGate = cirq.SwapGate() control = 0 controlFirst = False for j in range(len(circuit[0])): for i in range(len(circuit)): if circuit[i][j] == "Pauli-X-Gate": cirqCircuit.append(cirq.X(qubits[i])) elif circuit[i][j] == "Pauli-Y-Gate": cirqCircuit.append(cirq.Y(qubits[i])) elif circuit[i][j] == "Pauli-Z-Gate": cirqCircuit.append(cirq.Z(qubits[i])) elif circuit[i][j] == "Hadamard Gate": cirqCircuit.append(cirq.H(qubits[i])) elif circuit[i][j] == "S Gate": cirqCircuit.append(cirq.S(qubits[i])) elif circuit[i][j] == "T Gate": cirqCircuit.append(cirq.T(qubits[i])) elif circuit[i][j] == "Identity": pass elif circuit[i][j] == "CNot Gate": if controlFirst: cirqCircuit.append(cNotGate(control, qubits[i])) else: cirqCircuit.append(cNotGate(qubits[i + 1], qubits[i])) elif circuit[i][j] == "Swap Gate": cirqCircuit.append(cirq.swapGate(control, qubits[i])) elif circuit[i][j] == "Deutsch OracleC": pass elif circuit[i][j] == "Deutsch Oracle": if randint(0, 1) == 1: cirqCircuit.append(cNotGate(qubits[i - 1], qubits[i])) else: pass elif circuit[i][j] == "Fredkin Gate": cirqCircuit.append( cirq.CSWAP(qubits[i], qubits[i + 1], qubits[i + 2])) elif circuit[i][j] == "Toffoli Gate": cirqCircuit.append( cirq.TOFFOLI(qubits[i - 2], qubits[i - 1], qubits[i])) elif "Control" in circuit[i][j]: if not controlFirst: control = qubits[i] controlFirst = True elif "Measurement" in circuit[i][j]: cirqCircuit.append( cirq.measure(qubits[i], key=str(i) + " " + str(j))) if DEBUG: print( "Class: cirqSim Function: buildCirqCircuit Line: 42 Output: cirqCircuit after being completely build" ) print(cirqCircuit) return cirqCircuit
def test_text_to_qcircuit_diagrammable(): qubits = cirq.NamedQubit('x'), cirq.NamedQubit('y') g = cirq.SwapGate(half_turns=0.5) f = _TextToQCircuitDiagrammable(g) qubit_map = {q: i for i, q in enumerate(qubits)} actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs(known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, qubit_map=qubit_map)) name = '{\\text{SWAP}^{0.5}}' expected_info = cirq.CircuitDiagramInfo( ('\multigate{1}' + name, '\ghost' + name), exponent=0.5, connected=False) assert actual_info == expected_info g = cirq.SWAP f = _TextToQCircuitDiagrammable(g) qubit_map = {q: i for q, i in zip(qubits, (4, 3))} actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs(known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, qubit_map=qubit_map)) expected_info = cirq.CircuitDiagramInfo( ('\ghost{\\text{SWAP}}', '\multigate{1}{\\text{SWAP}}'), connected=False) assert actual_info == expected_info qubit_map = {q: i for q, i in zip(qubits, (2, 5))} actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs(known_qubits=qubits, known_qubit_count=None, use_unicode_characters=True, precision=3, qubit_map=qubit_map)) expected_info = cirq.CircuitDiagramInfo(('\\gate{\\text{×}}', ) * 2) assert actual_info == expected_info actual_info = f.qcircuit_diagram_info( cirq.CircuitDiagramInfoArgs.UNINFORMED_DEFAULT) assert actual_info == expected_info
def main(): # Pick a qubit. q0 = cirq.GridQubit(0, 0) q1 = cirq.GridQubit(0, 1) q2 = cirq.GridQubit(1, 0) q3 = cirq.GridQubit(1, 1) q4 = cirq.GridQubit(2, 0) q5 = cirq.GridQubit(2, 1) Swap = cirq.SwapGate() # Create a circuit circuit = cirq.Circuit.from_ops(cirq.measure(q0, key='q0 before swap'), cirq.measure(q1, key='q1 before swap'), Swap(q0, q1), cirq.measure(q0, key='q0 after swap'), cirq.measure(q1, key='q1 after swap'), cirq.X(q2), cirq.measure(q2, key='q2 before swap'), cirq.measure(q3, key='q3 before swap'), Swap(q2, q3), cirq.measure(q2, key='q2 after swap'), cirq.measure(q3, key='q3 after swap'), cirq.X(q5), cirq.measure(q4, key='q4 before swap'), cirq.measure(q5, key='q5 before swap'), Swap(q4, q5), cirq.measure(q4, key='q4 after swap'), cirq.measure(q5, key='q5 after swap')) print("Circuit:") print(circuit) # Simulate the circuit 50 times. simulator = cirq.google.XmonSimulator() result = simulator.run(circuit, repetitions=50) print("Results of simulation:") print(result) # Run the simulator with direct access to the wave function of the quantum processor resultw = simulator.simulate(circuit) print("Result from wave function:") print(resultw)