Exemple #1
0
 def neighbors_of(self, qubit: devices.LineQubit):
     """Returns the qubits that the given qubit can interact with."""
     possibles = [
         devices.LineQubit(qubit.x + 1),
         devices.LineQubit(qubit.x - 1),
     ]
     return [e for e in possibles if e in self.qubits]
def _init_ops(data: Dict[str, Any]) -> 'cirq.OP_TREE':
    if 'init' not in data:
        return []
    init = data['init']
    if not isinstance(init, List):
        raise ValueError(f'Circuit JSON init must be a list but was {init!r}.')
    init_ops = []
    for i in range(len(init)):
        state = init[i]
        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)
Exemple #3
0
def linearize_circuit_qubits(
        circuit: circuits.Circuit,
        qubit_order: ops.QubitOrderOrList = ops.QubitOrder.DEFAULT) -> None:
    qubits = ops.QubitOrder.as_qubit_order(qubit_order).order_for(
        circuit.all_qubits())
    qubit_map = {q: devices.LineQubit(i) for i, q in enumerate(qubits)}
    QubitMapper(qubit_map.__getitem__).optimize_circuit(circuit)
Exemple #4
0
def generate_library_of_2q_circuits(
    n_library_circuits: int,
    two_qubit_gate: 'cirq.Gate',
    *,
    max_cycle_depth: int = 100,
    q0: 'cirq.Qid' = devices.LineQubit(0),
    q1: 'cirq.Qid' = devices.LineQubit(1),
    random_state: 'cirq.RANDOM_STATE_OR_SEED_LIKE' = None,
) -> List['cirq.Circuit']:
    """Generate a library of two-qubit Circuits.

    For single-qubit gates, this uses PhasedXZGates where the axis-in-XY-plane is one
    of eight eighth turns and the Z rotation angle is one of eight eighth turns. This
    provides 8*8=64 total choices, each implementable with one PhasedXZGate. This is
    appropriate for architectures with microwave single-qubit control.

    Args:
        n_library_circuits: The number of circuits to generate.
        two_qubit_gate: The two qubit gate to use in the circuits.
        max_cycle_depth: The maximum cycle_depth in the circuits to generate. If you are using XEB,
            this must be greater than or equal to the maximum value in `cycle_depths`.
        q0: The first qubit to use when constructing the circuits.
        q1: The second qubit to use when constructing the circuits
        random_state: A random state or seed used to deterministically sample the random circuits.
    """
    rs = value.parse_random_state(random_state)
    exponents = np.linspace(0, 7 / 4, 8)
    single_qubit_gates = [
        ops.PhasedXZGate(x_exponent=0.5, z_exponent=z, axis_phase_exponent=a)
        for a, z in itertools.product(exponents, repeat=2)
    ]
    return [
        random_rotations_between_two_qubit_circuit(
            q0,
            q1,
            depth=max_cycle_depth,
            two_qubit_op_factory=lambda a, b, _: two_qubit_gate(a, b),
            single_qubit_gates=single_qubit_gates,
            seed=rs,
        )
        for _ in range(n_library_circuits)
    ]
Exemple #5
0
def _get_combinations_by_layer_for_isolated_xeb(
    circuits: Sequence['cirq.Circuit'],
) -> Tuple[List[CircuitLibraryCombination], List['cirq.Circuit']]:
    """Helper function used in `sample_2q_xeb_circuits`.

    This creates a CircuitLibraryCombination object for isolated XEB. First, the qubits
    are extracted from the lists of circuits and used to define one pair. Instead of using
    `combinations` to shuffle the circuits for each pair, we just use each circuit (in order)
    for the one pair.
    """
    q0, q1 = _verify_and_get_two_qubits_from_circuits(circuits)
    circuits = [
        circuit.transform_qubits(lambda q: {q0: devices.LineQubit(0), q1: devices.LineQubit(1)}[q])
        for circuit in circuits
    ]
    return [
        CircuitLibraryCombination(
            layer=None, combinations=np.arange(len(circuits))[:, np.newaxis], pairs=[(q0, q1)]
        )
    ], circuits
Exemple #6
0
    def connectivity(self) -> Set[Tuple[devices.LineQubit, devices.LineQubit]]:
        """Returns which qubits and can interact with which.

        Returns:
            A set of the possible qubits that can interact as tuples. This contains both
            ordered pairs. If `(cirq.LineQubit(x), cirq.LineQubit(y))` is in the set, then
            `(cirq.LineQubit(y), cirq.LineQubit(x))` is in the set.
        """
        connections = self._calibration_dict['connectivity']
        to_qubit = lambda x: devices.LineQubit(int(x))
        return set((to_qubit(x), to_qubit(y)) for x, y in connections).union(
            set((to_qubit(y), to_qubit(x)) for x, y in connections))
Exemple #7
0
def line_qubit_from_proto_id(proto_id: str) -> 'cirq.LineQubit':
    """Parse a proto id to a `cirq.LineQubit`.

    Proto ids for line qubits are integer strings representing the `x`
    attribute of the line qubit.

    Args:
        proto_id: The id to convert.

    Returns:
        A `cirq.LineQubit` corresponding to the proto id.

    Raises:
        ValueError: If the string is not an integer.
    """
    try:
        return devices.LineQubit(x=int(proto_id))
    except ValueError:
        raise ValueError(
            f'Line qubit proto id must be an int but was {proto_id}')
Exemple #8
0
 def at(self, position: int) -> Optional[devices.LineQubit]:
     """Returns the qubit at the given position, if there is one, else None.
     """
     q = devices.LineQubit(position)
     return q if q in self.qubits else None