How to use the cirq.circuits.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 / experiments / cross_entropy_benchmarking.py View on Github external
single_qubit_gates: List[List[ops.SingleQubitGate]] = None,
        benchmark_ops: Sequence[ops.Moment] = None,
) -> List[circuits.Circuit]:
    if benchmark_ops is not None:
        num_d = len(benchmark_ops)
    else:
        num_d = 0
    max_cycles = max(cycles)

    if single_qubit_gates is None:
        single_rots = _random_half_rotations(qubits, max_cycles)
    else:
        single_rots = _random_any_gates(qubits, single_qubit_gates, max_cycles)
    all_circuits = []  # type: List[circuits.Circuit]
    for num_cycles in cycles:
        circuit_exp = circuits.Circuit()
        for i in range(num_cycles):
            circuit_exp.append(single_rots[i])
            if benchmark_ops is not None:
                for op_set in benchmark_ops[i % num_d]:
                    circuit_exp.append(op_set)
        all_circuits.append(circuit_exp)
    return all_circuits
github quantumlib / Cirq / cirq / ion / ion_decomposition.py View on Github external
def _cleanup_operations(operations: List[ops.Operation]):
    circuit = circuits.Circuit(operations)
    optimizers.merge_single_qubit_gates.\
        merge_single_qubit_gates_into_phased_x_z(circuit)
    optimizers.eject_phased_paulis.EjectPhasedPaulis().optimize_circuit(circuit)
    optimizers.eject_z.EjectZ().optimize_circuit(circuit)
    circuit = circuits.Circuit(circuit.all_operations(),
                               strategy=circuits.InsertStrategy.EARLIEST)
    return list(circuit.all_operations())
github quantumlib / Cirq / cirq / schedules / schedule.py View on Github external
def to_circuit(self) -> Circuit:
        """Convert the schedule to a circuit.

        This discards most timing information from the schedule, but does place
        operations that are scheduled at the same time in the same Moment.
        """
        circuit = Circuit(device=self.device)
        time: Optional[Timestamp] = None
        for so in self.scheduled_operations:
            if so.time != time:
                circuit.append(so.operation,
                               strategy=InsertStrategy.NEW_THEN_INLINE)
                time = so.time
            else:
                circuit.append(so.operation,
                               strategy=InsertStrategy.INLINE)
        return circuit
github quantumlib / Cirq / cirq / contrib / routing / greedy.py View on Github external
def swap_network(self) -> SwapNetwork:
        return SwapNetwork(circuits.Circuit(self.physical_ops),
                           self.initial_mapping)
github quantumlib / Cirq / cirq / google / serializable_gate_set.py View on Github external
) -> 'cirq.Circuit':
        moments = []
        for i, moment_proto in enumerate(circuit_proto.moments):
            moment_ops = []
            for op in moment_proto.operations:
                try:
                    moment_ops.append(
                        self.deserialize_op(
                            op, arg_function_language=arg_function_language))
                except ValueError as ex:
                    raise ValueError(f'Failed to deserialize circuit. '
                                     f'There was a problem in moment {i} '
                                     f'handling an operation with the '
                                     f'following proto:\n{op}') from ex
            moments.append(ops.Moment(moment_ops))
        return circuits.Circuit(moments)
github quantumlib / Cirq / cirq / interop / quirk / cells / composite_cell.py View on Github external
def circuit(self) -> 'cirq.Circuit':
        result = circuits.Circuit()
        for col in self._sub_cell_cols_sealed():
            body = circuits.Circuit(
                cell.operations() for cell in col if cell is not None)
            if body:
                basis_change = circuits.Circuit(
                    cell.basis_change() for cell in col if cell is not None)
                result += basis_change
                result += body
                result += basis_change**-1
        return result
github quantumlib / Cirq / cirq / sim / mux.py View on Github external
is determined by the qubit order argument (which defaults to just
        sorting the qubits that are present into an ascending order).
    """

    if not isinstance(initial_state, int):
        initial_state = np.asarray(initial_state, dtype=dtype)

    if isinstance(program, (schedules.Schedule, circuits.Circuit)):
        # No change needed.
        pass
    elif isinstance(program, ops.Gate):
        program = circuits.Circuit(
            program.on(*devices.LineQid.for_gate(program)))
    else:
        # It should be an OP_TREE.
        program = circuits.Circuit(program)

    if not protocols.has_unitary(
            protocols.resolve_parameters(program, param_resolver)):
        raise ValueError(
            "Program doesn't have a single well defined final wavefunction "
            "because it is not unitary. "
            "Maybe you wanted `cirq.sample_wavefunction`?\n"
            "\n"
            "Program: {!r}".format(program))

    result = sparse_simulator.Simulator(dtype=dtype, seed=seed).simulate(
        program=cast(Union[circuits.Circuit, schedules.Schedule], program),
        initial_state=initial_state,
        qubit_order=qubit_order,
        param_resolver=param_resolver)