Exemple #1
0
def test_sympy_qudits():
    q0 = cirq.LineQid(0, 3)
    q1 = cirq.LineQid(1, 5)
    q_result = cirq.LineQubit(2)

    class PlusGate(cirq.Gate):
        def __init__(self, dimension, increment=1):
            self.dimension = dimension
            self.increment = increment % dimension

        def _qid_shape_(self):
            return (self.dimension, )

        def _unitary_(self):
            inc = (self.increment - 1) % self.dimension + 1
            u = np.empty((self.dimension, self.dimension))
            u[inc:] = np.eye(self.dimension)[:-inc]
            u[:inc] = np.eye(self.dimension)[-inc:]
            return u

    for i in range(15):
        digits = cirq.big_endian_int_to_digits(i, digit_count=2, base=(3, 5))
        circuit = cirq.Circuit(
            PlusGate(3, digits[0]).on(q0),
            PlusGate(5, digits[1]).on(q1),
            cirq.measure(q0, q1, key='m'),
            cirq.X(q_result).with_classical_controls(
                sympy_parser.parse_expr('m % 4 <= 1')),
            cirq.measure(q_result, key='m_result'),
        )

        result = cirq.Simulator().run(circuit)
        assert result.measurements['m_result'][0][0] == (i % 4 <= 1)
Exemple #2
0
 def _apply_unitary(self, op, unitary, op_shape, state, indices):
     indices = list(indices)
     target_state = state[indices]
     target_val = cirq.big_endian_digits_to_int(target_state, base=op_shape)
     result_wavefunction = unitary[:, target_val]
     result_val = np.argmax(np.abs(result_wavefunction))
     if not (np.isclose(np.abs(result_wavefunction[result_val]), 1)
             and np.sum(1 - np.isclose(result_wavefunction, 0)) == 1):
         # The output state vector does not represent a single basis state
         raise ValueError(
             "Can't simulate non-classical operations. "
             "The operation's unitary is not a permutation matrix: "
             "{!r}\n{!r}".format(op, unitary))
     result_state = cirq.big_endian_int_to_digits(result_val, base=op_shape)
     state[indices] = result_state
Exemple #3
0
    def _base_iterator(self,
                       circuit,
                       qubit_order,
                       initial_state,
                       perform_measurements=True):
        qubits = cirq.QubitOrder.as_qubit_order(qubit_order).order_for(
            circuit.all_qubits())
        num_qubits = len(qubits)
        qid_shape = cirq.qid_shape(qubits)
        qubit_map = {q: i for i, q in enumerate(qubits)}
        if isinstance(initial_state, int):
            state_val = initial_state
        else:
            state_val = cirq.big_endian_digits_to_int(initial_state,
                                                      base=qid_shape)
        state = np.array(list(
            cirq.big_endian_int_to_digits(state_val, base=qid_shape)),
                         dtype=np.uint8)
        if len(circuit) == 0:
            yield ClassicalSimulatorStep(state, {}, qubit_map)

        def on_stuck(bad_op):
            return TypeError(
                "Can't simulate unknown operations that don't specify a "
                "unitary or a decomposition. {!r}".format(bad_op))

        def keep(op):
            return ((cirq.num_qubits(op) <= 32 and
                     (cirq.has_unitary(op) or cirq.has_mixture(op)))
                    or cirq.is_measurement(op)
                    or isinstance(op.gate, cirq.ResetChannel))

        def simulate_op(op, temp_state):
            indices = [qubit_map[q] for q in op.qubits]
            if isinstance(op.gate, cirq.ResetChannel):
                self._simulate_reset(op, temp_state, indices)
            elif cirq.is_measurement(op):
                if perform_measurements:
                    self._simulate_measurement(op, temp_state, indices,
                                               measurements)
            else:
                decomp_ops = cirq.decompose_once(op, default=None)
                if decomp_ops is None:
                    self._simulate_from_matrix(op, temp_state, indices)
                else:
                    try:
                        temp2_state = temp_state.copy()
                        for sub_op in cirq.flatten_op_tree(decomp_ops):
                            simulate_op(sub_op, temp2_state)
                        temp_state[...] = temp2_state
                    except ValueError:
                        # Non-classical unitary in the decomposition
                        self._simulate_from_matrix(op, temp_state, indices)

        for moment in circuit:
            measurements = defaultdict(list)
            known_ops = cirq.decompose(moment,
                                       keep=keep,
                                       on_stuck_raise=on_stuck)
            for op in known_ops:
                simulate_op(op, state)
            yield ClassicalSimulatorStep(state.copy(), measurements, qubit_map)
Exemple #4
0
def test_big_endian_int_to_digits():
    with pytest.raises(ValueError, match='No digit count'):
        _ = cirq.big_endian_int_to_digits(0, base=10)
    with pytest.raises(ValueError, match='Inconsistent digit count'):
        _ = cirq.big_endian_int_to_digits(0, base=[], digit_count=1)
    with pytest.raises(ValueError, match='Inconsistent digit count'):
        _ = cirq.big_endian_int_to_digits(0, base=[2, 3], digit_count=20)
    with pytest.raises(ValueError, match='Out of range'):
        assert cirq.big_endian_int_to_digits(101, base=[], digit_count=0) == []
    with pytest.raises(ValueError, match='Out of range'):
        _ = cirq.big_endian_int_to_digits(10**100, base=[2, 3, 4, 5, 6])

    assert cirq.big_endian_int_to_digits(0, base=[], digit_count=0) == []
    assert cirq.big_endian_int_to_digits(0, base=[]) == []
    assert cirq.big_endian_int_to_digits(11, base=3,
                                         digit_count=4) == [0, 1, 0, 2]
    assert cirq.big_endian_int_to_digits(11, base=[3, 3, 3, 3],
                                         digit_count=4) == [0, 1, 0, 2]
    assert cirq.big_endian_int_to_digits(11, base=[2, 3, 4],
                                         digit_count=3) == [0, 2, 3]

    # Use-once base iterators.
    assert cirq.big_endian_int_to_digits(11,
                                         base=(e for e in [2, 3, 4]),
                                         digit_count=3) == [0, 2, 3]
    assert cirq.big_endian_int_to_digits(
        11, base=(e for e in [2, 3, 4])) == [0, 2, 3]
Exemple #5
0
 def from_basis_state(int_state, qid_shape, *args, **kwargs):
     digits = cirq.big_endian_int_to_digits(int_state, base=qid_shape)
     state = Counter({tuple(digits): 1})
     return _State(qid_shape, *args, **kwargs, state=state)