How to use the cirq.measure function in cirq

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 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 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')
github quantumlib / Cirq / dev_tools / profiling / benchmark_simulators.py View on Github external
q1 = cirq.GridQubit(0, np.random.randint(num_qubits))
            circuit.append(
                cirq.PhasedXPowGate(
                    phase_exponent=np.random.random(),
                    exponent=np.random.random()
                ).on(q1))
        elif which == 'expz':
            q1 = cirq.GridQubit(0, np.random.randint(num_qubits))
            circuit.append(
                cirq.Z(q1)**np.random.random())
        elif which == 'exp11':
            q1 = cirq.GridQubit(0, np.random.randint(num_qubits - 1))
            q2 = cirq.GridQubit(0, q1.col + 1)
            circuit.append(cirq.CZ(q1, q2)**np.random.random())
    circuit.append([
        cirq.measure(cirq.GridQubit(0, np.random.randint(num_qubits)),
                     key='meas')
    ])

    if sim_type == _UNITARY:
        circuit.final_wavefunction(initial_state=0)
    elif sim_type == _DENSITY:
        cirq.DensityMatrixSimulator().run(circuit)
github JackHidary / quantumcomputingbook / chapter09 / cirq / hhl.py View on Github external
c = cirq.Circuit()
    hs = HamiltonianSimulation(A, t)
    pe = PhaseEstimation(register_size+1, hs)
    c.append([gate(memory) for gate in input_prep_gates])
    c.append([
        pe(*(register + [memory])),
        EigenRotation(register_size + 1, C, t)(*(register + [ancilla])),
        pe(*(register + [memory]))**-1,
        cirq.measure(ancilla, key='a')
    ])

    c.append([
        cirq.PhasedXPowGate(
            exponent=sympy.Symbol('exponent'),
            phase_exponent=sympy.Symbol('phase_exponent'))(memory),
        cirq.measure(memory, key='m')
    ])

    return c
github quantumlib / Cirq / examples / quantum_teleportation.py View on Github external
def make_quantum_teleportation_circuit(ranX, ranY):
    circuit = cirq.Circuit()
    msg, alice, bob = cirq.LineQubit.range(3)

    # Creates Bell state to be shared between Alice and Bob
    circuit.append([cirq.H(alice), cirq.CNOT(alice, bob)])
    # Creates a random state for the Message
    circuit.append([cirq.X(msg)**ranX, cirq.Y(msg)**ranY])
    # Bell measurement of the Message and Alice's entangled qubit
    circuit.append([cirq.CNOT(msg, alice), cirq.H(msg)])
    circuit.append(cirq.measure(msg, alice))
    # Uses the two classical bits from the Bell measurement to recover the
    # original quantum Message on Bob's entangled qubit
    circuit.append([cirq.CNOT(alice, bob), cirq.CZ(msg, bob)])

    return circuit
github quantumlib / Cirq / examples / qaoa.py View on Github external
def qaoa_max_cut_circuit(qubits, betas, gammas,
                         graph):  # Nodes should be integers
    return cirq.Circuit(
        # Prepare uniform superposition
        cirq.H.on_each(*qubits),
        # Apply QAOA unitary
        qaoa_max_cut_unitary(qubits, betas, gammas, graph),
        # Measure
        cirq.measure(*qubits, key='m'))
github quantumlib / Cirq / cirq / contrib / quantum_volume / quantum_volume.py View on Github external
# Add measure gates to the end of (a copy of) the circuit. Ensure that those
    # gates measure those in the given mapping, preserving this order.
    qubits = circuit.all_qubits()
    key = None
    if mapping:
        # Add any qubits that were not explicitly mapped, so they aren't lost in
        # the sorting.
        key = lambda q: mapping.get(q, q)
        qubits = frozenset(mapping.keys())

    # Don't do a single large measurement gate because then the key will be one
    # large string. Instead, do a bunch of single-qubit measurement gates so we
    # preserve the qubit keys.
    sorted_qubits = sorted(qubits, key=key)
    circuit_copy = circuit + [cirq.measure(q) for q in sorted_qubits]

    # Run the sampler to compare each output against the Heavy Set.
    trial_result = sampler.run(program=circuit_copy, repetitions=repetitions)

    # Post-process the results, e.g. to handle error corrections.
    results = process_results(mapping, compilation_result.parity_map,
                              trial_result)

    # Aggregate the results into bit-strings (since we are using individual
    # measurement gates).

    results = results.agg(lambda meas: cirq.value.big_endian_bits_to_int(meas),
                          axis=1)
    # Compute the number of outputs that are in the heavy set.
    num_in_heavy_set = np.sum(np.in1d(results, heavy_set))
github JackHidary / quantumcomputingbook / chapter08 / cirq / grover.py View on Github external
])

    # Query oracle.
    c.append(oracle)

    # Construct Grover operator.
    c.append(cirq.H.on_each(*input_qubits))
    c.append(cirq.X.on_each(*input_qubits))
    c.append(cirq.H.on(input_qubits[1]))
    c.append(cirq.CNOT(input_qubits[0], input_qubits[1]))
    c.append(cirq.H.on(input_qubits[1]))
    c.append(cirq.X.on_each(*input_qubits))
    c.append(cirq.H.on_each(*input_qubits))

    # Measure the result.
    c.append(cirq.measure(*input_qubits, key='result'))

    return c