How to use the cirq.ops.Moment 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 / interop / quirk / url_to_circuit.py View on Github external
q = devices.LineQubit(i)
        if state == 0:
            pass
        elif state == 1:
            init_ops.append(ops.X(q))
        elif state == '+':
            init_ops.append(ops.ry(np.pi / 2).on(q))
        elif state == '-':
            init_ops.append(ops.ry(-np.pi / 2).on(q))
        elif state == 'i':
            init_ops.append(ops.rx(-np.pi / 2).on(q))
        elif state == '-i':
            init_ops.append(ops.rx(np.pi / 2).on(q))
        else:
            raise ValueError(f'Unrecognized init state: {state!r}')
    return ops.Moment(init_ops)
github quantumlib / Cirq / cirq / google / serializable_gate_set.py View on Github external
arg_function_language: str,
    ) -> '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 / circuits / circuit.py View on Github external
def _resolve_parameters_(self,
                             param_resolver: 'cirq.ParamResolver') -> 'Circuit':
        resolved_moments = []
        for moment in self:
            resolved_operations = _resolve_operations(
                moment.operations,
                param_resolver)
            new_moment = ops.Moment(resolved_operations)
            resolved_moments.append(new_moment)
        resolved_circuit = Circuit(resolved_moments, device=self.device)
        return resolved_circuit
github quantumlib / Cirq / cirq / contrib / paulistring / separate.py View on Github external
def regular_half(circuit: circuits.Circuit) -> circuits.Circuit:
    """Return only the Clifford part of a circuit.  See
    convert_and_separate_circuit().

    Args:
        circuit: A Circuit with the gate set {SingleQubitCliffordGate,
            PauliInteractionGate, PauliStringPhasor}.

    Returns:
        A Circuit with SingleQubitCliffordGate and PauliInteractionGate gates.
        It also contains MeasurementGates if the given
        circuit contains measurements.
    """
    return circuits.Circuit(
        ops.Moment(op
                   for op in moment.operations
                   if not isinstance(op, ops.PauliStringPhasor))
        for moment in circuit)
github quantumlib / Cirq / cirq / circuits / circuit.py View on Github external
Args:
            splitter_index: The index to insert at.
            op: The operation that will be inserted.
            strategy: The insertion strategy.

        Returns:
            The index of the (possibly new) moment where the insertion should
                occur.

        Raises:
            ValueError: Unrecognized append strategy.
        """

        if (strategy is InsertStrategy.NEW or
                strategy is InsertStrategy.NEW_THEN_INLINE):
            self._moments.insert(splitter_index, ops.Moment())
            return splitter_index

        if strategy is InsertStrategy.INLINE:
            if (0 <= splitter_index - 1 < len(self._moments) and
                    self._can_add_op_at(splitter_index - 1, op)):
                return splitter_index - 1

            return self._pick_or_create_inserted_op_moment_index(
                splitter_index, op, InsertStrategy.NEW)

        if strategy is InsertStrategy.EARLIEST:
            if self._can_add_op_at(splitter_index, op):
                p = self._prev_moment_available(op, splitter_index)
                return p or 0

            return self._pick_or_create_inserted_op_moment_index(
github quantumlib / Cirq / cirq / work / pauli_sum_collector.py View on Github external
def _circuit_plus_pauli_string_measurements(circuit: circuits.Circuit,
                                            pauli_string: ops.PauliString
                                           ) -> circuits.Circuit:
    """A circuit measuring the given observable at the end of the given circuit.
    """
    assert pauli_string
    circuit = circuit.copy()
    circuit.append(ops.Moment(pauli_string.to_z_basis_ops()))
    circuit.append(
        ops.Moment([ops.measure(*sorted(pauli_string.keys()), key='out')]))
    return circuit
github quantumlib / Cirq / cirq / circuits / circuit.py View on Github external
self._device.validate_operation(
                    cast(ops.Operation, moment_or_op))
            self._validate_op_tree_qids(moment_or_op)

        # limit index to 0..len(self._moments), also deal with indices smaller 0
        k = max(min(index if index >= 0 else len(self._moments) + index,
                    len(self._moments)), 0)
        for moment_or_op in moments_and_operations:
            if isinstance(moment_or_op, ops.Moment):
                self._moments.insert(k, moment_or_op)
                k += 1
            else:
                p = self._pick_or_create_inserted_op_moment_index(
                    k, moment_or_op, strategy)
                while p >= len(self._moments):
                    self._moments.append(ops.Moment())
                self._moments[p] = self._moments[p].with_operation(moment_or_op)
                self._device.validate_moment(self._moments[p])
                k = max(k, p + 1)
                if strategy is InsertStrategy.NEW_THEN_INLINE:
                    strategy = InsertStrategy.INLINE
        return k