How to use cirq - 10 common examples

To help you get started, we’ve selected a few cirq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gecrooks / quantumflow-dev / tests / test_xcirq.py View on Github external
def test_cirq_to_circuit2():

    q0 = cq.GridQubit(0, 0)
    q1 = cq.GridQubit(1, 0)

    def basic_circuit(meas=False):
        sqrt_x = cq.X**0.5
        yield cq.X(q0) ** 0.5, sqrt_x(q1)
        yield cq.CZ(q0, q1)
        yield sqrt_x(q0), sqrt_x(q1)
        if meas:
            yield cq.measure(q0, key='q0'), cq.measure(q1, key='q1')

    cqc = cq.Circuit()
    cqc.append(basic_circuit())

    print()
    print(cqc)
github ngnrsaa / qflex / tests / python / simulator.py View on Github external
for x in grid_stream.readlines()
            if len(x) and x[0] != '#']

    # Get the number of rows
    grid_I = len(grid)

    # Check that the number of columns is consistent
    if len(set(len(x) for x in grid)) != 1:
        raise AssertionError("Number of columns in grid are not consistent.")

    # Get the number of columns
    grid_J = len(grid[0])

    # Return cirq.GridQubit
    return [
        cirq.GridQubit(I, J)
        for I in range(grid_I)
        for J in range(grid_J)
        if grid[I][J] == '1'
    ]
github XanaduAI / pennylane-cirq / tests / test_cirq_device.py View on Github external
def test_outer_init_of_qubits_error(self):
        """Tests that giving the wrong number of qubits as parameters to CirqDevice raises an error."""

        qubits = [
            cirq.GridQubit(0, 0),
            cirq.GridQubit(0, 1),
            cirq.GridQubit(1, 0),
            cirq.GridQubit(1, 1),
        ]

        with pytest.raises(
            qml.DeviceError,
            match="The number of given qubits and the specified number of wires have to match",
        ):
            dev = CirqDevice(3, 100, False, qubits=qubits)
github gecrooks / quantumflow-dev / tests / test_xcirq.py View on Github external
def test_cirq_to_circuit():
    q0 = cq.LineQubit(0)
    q1 = cq.LineQubit(1)
    q2 = cq.LineQubit(2)

    gate = cirq_to_circuit(cq.Circuit(cq.X(q0)))[0]
    assert isinstance(gate, qf.X)
    assert gate.qubits == (0,)

    gate = cirq_to_circuit(cq.Circuit(cq.X(q1)**0.4))[0]
    assert isinstance(gate, qf.TX)
    assert gate.qubits == (1,)

    gate = cirq_to_circuit(cq.Circuit(cq.CZ(q1, q0)))[0]
    assert isinstance(gate, qf.CZ)
    assert gate.qubits == (1, 0)

    gate = cirq_to_circuit(cq.Circuit(cq.CZ(q1, q0) ** 0.3))[0]
    assert isinstance(gate, qf.CZPow)
    assert gate.qubits == (1, 0)
    assert gate.params['t'] == 0.3
github gecrooks / quantumflow-dev / tests / test_xcirq.py View on Github external
def basic_circuit(meas=False):
        sqrt_x = cq.X**0.5
        yield cq.X(q0) ** 0.5, sqrt_x(q1)
        yield cq.CZ(q0, q1)
        yield sqrt_x(q0), sqrt_x(q1)
        if meas:
            yield cq.measure(q0, key='q0'), cq.measure(q1, key='q1')
github ngnrsaa / qflex / tests / python / simulator.py View on Github external
'']
    verbose = args['--verbose']

    # Get grid from file
    with open(grid_filename) as grid_stream:
        qubits = GetGridQubit(grid_stream)

    # Get circuit from file
    with open(circuit_filename) as circuit_stream:
        circuit = GetCircuit(circuit_stream, qubits)

    # Print quantum circuit
    if verbose: print(circuit, file=stderr)

    # Print amplitudes
    simulator = cirq.Simulator()
    results = simulator.simulate(circuit, qubit_order=qubits)

    for x, v in enumerate(results.final_state):
        print(('{{: {}d}}: |{{}}>'.format(len(str(len(qubits))) + 2)).format(
            x,
            bin(x)[2:].zfill(len(qubits))[::-1]), v)
github gecrooks / quantumflow-dev / tests / test_xcirq.py View on Github external
def test_cirq_to_circuit():
    q0 = cq.LineQubit(0)
    q1 = cq.LineQubit(1)
    q2 = cq.LineQubit(2)

    gate = cirq_to_circuit(cq.Circuit(cq.X(q0)))[0]
    assert isinstance(gate, qf.X)
    assert gate.qubits == (0,)

    gate = cirq_to_circuit(cq.Circuit(cq.X(q1)**0.4))[0]
    assert isinstance(gate, qf.TX)
    assert gate.qubits == (1,)

    gate = cirq_to_circuit(cq.Circuit(cq.CZ(q1, q0)))[0]
    assert isinstance(gate, qf.CZ)
    assert gate.qubits == (1, 0)

    gate = cirq_to_circuit(cq.Circuit(cq.CZ(q1, q0) ** 0.3))[0]
    assert isinstance(gate, qf.CZPow)
    assert gate.qubits == (1, 0)
github quantumlib / Cirq / cirq / testing / consistent_qasm.py View on Github external
import qiskit
    except ImportError:
        # coverage: ignore
        warnings.warn("Skipped assert_qasm_is_consistent_with_unitary because "
                      "qiskit isn't installed to verify against.")
        return

    unitary = protocols.unitary(val, None)
    if unitary is None:
        # Vacuous consistency.
        return

    if isinstance(val, ops.Operation):
        qubits: Sequence[ops.Qid] = val.qubits
        op = val
    elif isinstance(val, ops.Gate):
        qid_shape = protocols.qid_shape(val)
        remaining_shape = list(qid_shape)
        controls = getattr(val, 'control_qubits', None)
        if controls is not None:
            for i, q in zip(reversed(range(len(controls))), reversed(controls)):
                if q is not None:
                    remaining_shape.pop(i)
        qubits = devices.LineQid.for_qid_shape(remaining_shape)
        op = val.on(*qubits)
    else:
        raise NotImplementedError("Don't know how to test {!r}".format(val))

    args = protocols.QasmArgs(
        qubit_id_map={q: 'q[{}]'.format(i) for i, q in enumerate(qubits)})
    qasm = protocols.qasm(op, args=args, default=None)
    if qasm is None:
github XanaduAI / pennylane-cirq / tests / test_cirq_device.py View on Github external
            (qml.PauliX(wires=[0]).inv(), [cirq.X ** -1]),
            (qml.PauliY(wires=[0]).inv(), [cirq.Y ** -1]),
            (qml.PauliZ(wires=[0]).inv(), [cirq.Z ** -1]),
            (qml.Hadamard(wires=[0]), [cirq.H]),
            (qml.Hadamard(wires=[0]).inv(), [cirq.H ** -1]),
            (qml.S(wires=[0]), [cirq.S]),
            (qml.S(wires=[0]).inv(), [cirq.S ** -1]),
            (qml.PhaseShift(1.4, wires=[0]), [cirq.ZPowGate(exponent=1.4 / np.pi)]),
            (qml.PhaseShift(-1.2, wires=[0]), [cirq.ZPowGate(exponent=-1.2 / np.pi)]),
            (qml.PhaseShift(2, wires=[0]), [cirq.ZPowGate(exponent=2 / np.pi)]),
            (
                qml.PhaseShift(1.4, wires=[0]).inv(),
                [cirq.ZPowGate(exponent=-1.4 / np.pi)],
            ),
            (
                qml.PhaseShift(-1.2, wires=[0]).inv(),
                [cirq.ZPowGate(exponent=1.2 / np.pi)],
github quantumlib / OpenFermion-Cirq / openfermioncirq / testing / example_classes.py View on Github external
def operations(self, qubits: Sequence[cirq.Qid]) -> cirq.OP_TREE:
        a, b = qubits
        yield cirq.XPowGate(exponent=sympy.Symbol('theta0')).on(a)
        yield cirq.XPowGate(exponent=sympy.Symbol('theta1')).on(b)
        yield cirq.CZ(a, b)
        yield cirq.XPowGate(exponent=sympy.Symbol('theta0')).on(a)
        yield cirq.XPowGate(exponent=sympy.Symbol('theta1')).on(b)
        yield cirq.measure(a, b, key='all')