How to use the cirq.Circuit 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 quantumlib / Cirq / cirq / aqt / aqt_device.py View on Github external
def __init__(self,
                 num_qubits: int,
                 circuit: Circuit = Circuit(),
                 simulate_ideal: bool = False,
                 noise_dict: Union[dict, None] = None):
        """Initializes the AQT simulator
        Args:
            num_qubits: Number of qubits
            circuit: Optional, circuit to be simulated.
            Last moment needs to be a measurement over all qubits with key 'm'
            simulate_ideal: If True, an ideal circuit will be simulated
        """
        self.circuit = circuit
        self.num_qubits = num_qubits
        self.qubit_list = LineQubit.range(num_qubits)
        if noise_dict is None:
            noise_dict = get_default_noise_dict()
        self.noise_dict = noise_dict
        self.simulate_ideal = simulate_ideal
github JackHidary / quantumcomputingbook / chapter08 / cirq / bernstein-vazirani.py View on Github external
def make_bernstein_vazirani_circuit(input_qubits, output_qubit, oracle):
    """Solves for factors in f(a) = a*factors + bias (mod 2) with one query."""
    c = cirq.Circuit()
    
    # Initialize qubits
    c.append([
        cirq.X(output_qubit),
        cirq.H(output_qubit),
        cirq.H.on_each(*input_qubits),
    ])

    # Query oracle
    c.append(oracle)

    # Measure in X basis
    c.append([
        cirq.H.on_each(*input_qubits),
        cirq.measure(*input_qubits, key='result')
    ])
github zapatacomputing / cusp_cirq_demo / cusp / cusp_stage1.py View on Github external
def state_prep_circuit(alpha, exact=False):
    """Returns state preparation circuit.

    Args:
    =====
    alpha : numeric
        Parameter for state preparation circuit
    exact : bool
        If True, works with wavefunction

    Returns:
    ========
    sp_circuit : cirq.Circuit
        State preparation circuit
    """
    sp_circuit = Circuit()
    sp_circuit.append(_input_prep_gates(alpha))
    if exact is False:
        sp_circuit.append([MeasurementGate('r00').on(q00),
                          MeasurementGate('r01').on(q01),
                          MeasurementGate('r10').on(q10),
                          MeasurementGate('r11').on(q11)])
    return sp_circuit
github quantumlib / OpenFermion-Cirq / openfermioncirq / variational / variational_black_box.py View on Github external
def __init__(self,
                 ansatz: VariationalAnsatz,
                 objective: VariationalObjective,
                 preparation_circuit: Optional[cirq.Circuit]=None,
                 initial_state: Union[int, numpy.ndarray]=0,
                 **kwargs) -> None:
        self.ansatz = ansatz
        self.objective = objective
        self.preparation_circuit = preparation_circuit or cirq.Circuit()
        self.initial_state = initial_state
        super().__init__(**kwargs)
github qctrl / python-open-controls / qctrlopencontrols / cirq / cirq_circuit.py View on Github external
the desired decoupling pulses are placed at offsets and the duration between
        the pulses are constructed from identity gates with delays equal to 'gate_time'.

    Raises
    ------
    ArgumentsValueError
        If there is rotations around more than one axis at any of the offsets
    """

    unitary_time = gate_time
    circuit_gate_list = _get_circuit_gate_list(
        dynamic_decoupling_sequence=dynamic_decoupling_sequence,
        gate_time=gate_time,
        unitary_time=unitary_time)

    circuit = cirq.Circuit()

    if pre_post_gate is not None:
        gate_list = []
        for qubit in target_qubits:
            gate_list.append(pre_post_gate(qubit))
        circuit.append(gate_list)

    offset_count = 0
    for gate in circuit_gate_list:

        if gate == 'id':
            gate_list = []
            for qubit in target_qubits:
                gate_list.append(cirq.I(qubit))
            circuit.append(gate_list)
            continue
github quantumlib / OpenFermion-Cirq / openfermioncirq / example.py View on Github external
def make_circuit_diagram():
    q = cirq.NamedQubit('q')
    r = cirq.NamedQubit('r')
    c = cirq.Circuit.from_ops(
        cirq.X(q),
        cirq.CZ(q, r)
    )
    return str(c)
github zapatacomputing / cusp_cirq_demo / cusp / cusp_stage3.py View on Github external
def decoder_circuit(aht, ht, zz, exact=False):
    """Returns latent space circuit followed by decoding circuit.

    Args:
    =====
    aht, ht, zz : numeric
        Parameters for latent space circuit
    exact : bool
        If True, works with wavefunction

    Returns:
    ========
    dc_circuit : cirq.Circuit
        Decoding circuit
    """
    dc_circuit = Circuit()
    dc_circuit.append(_latent_space_circuit_gates(aht, ht, zz))
    dc_circuit.append(param_CNOT(a, b, x, z, q11, q01))
    dc_circuit.append(param_CNOT(a, b, x, z, q01, q00))
    dc_circuit.append(param_CNOT(a, b, x, z, q11, q10), strategy=InsertStrategy.EARLIEST)
    dc_circuit.append([X(q11), X(q10)])
    if exact is False:
        dc_circuit.append([MeasurementGate('r00').on(q00),
                          MeasurementGate('r01').on(q01),
                          MeasurementGate('r10').on(q10),
                          MeasurementGate('r11').on(q11)])
    return dc_circuit
github quantumlib / Cirq / cirq / experiments / n_qubit_tomography.py View on Github external
self.num_qubits = len(qubits)

        phase_exp_vals, exp_vals = zip(*prerotations)

        ops: List[cirq.Operation] = []
        sweeps: List[cirq.Sweep] = []
        for i, qubit in enumerate(qubits):
            phase_exp = sympy.Symbol(f'phase_exp_{i}')
            exp = sympy.Symbol(f'exp_{i}')
            gate = cirq.PhasedXPowGate(phase_exponent=phase_exp, exponent=exp)
            ops.append(gate.on(qubit))
            sweeps.append(
                cirq.Points(phase_exp, phase_exp_vals) +
                cirq.Points(exp, exp_vals))

        self.rot_circuit = cirq.Circuit(ops)
        self.rot_sweep = cirq.Product(*sweeps)
        self.mat = self._make_state_tomography_matrix()
github quantumlib / Cirq / examples / qubit_characterizations_example.py View on Github external
simulator, q_0, num_clifford_range=num_cfds, repetitions=100)
    rb_result_1q.plot()

    # Clifford-based randomized benchmarking of two-qubit gates on q_0 and q_1.
    rb_result_2q = cirq.experiments.two_qubit_randomized_benchmarking(
        simulator, q_0, q_1, num_clifford_range=num_cfds, repetitions=100)
    rb_result_2q.plot()

    # State-tomography of q_0 after application of an X/2 rotation.
    cir_1q = cirq.Circuit.from_ops(cirq.X(q_0) ** 0.5)
    tomography_1q = cirq.experiments.single_qubit_state_tomography(simulator,
                                                                   q_0, cir_1q)
    tomography_1q.plot()

    # State-tomography of a Bell state between q_0 and q_1.
    cir_2q = cirq.Circuit.from_ops(cirq.H(q_0), cirq.CNOT(q_0, q_1))
    tomography_2q = cirq.experiments.two_qubit_state_tomography(simulator,
                                                                q_0, q_1,
                                                                cir_2q)
    tomography_2q.plot()