示例#1
0
def test_iadd_isub():
    q = qp.Quint(qp.NamedQureg('test', 10))

    with pytest.raises(TypeError):
        q += None

    with qp.RandomSim(measure_bias=0.5):
        with qp.capture() as out:
            q += 5
    assert qp.ccz_count(out) == 18

    with qp.RandomSim(measure_bias=0.5):
        with qp.capture() as out:
            q += 4
    assert qp.ccz_count(out) == 14

    with qp.RandomSim(measure_bias=0.5):
        with qp.capture() as out:
            q -= 3
    assert qp.ccz_count(out) == 18

    q2 = qp.Quint(qp.NamedQureg('test2', 5))
    with qp.RandomSim(measure_bias=0.5):
        with qp.capture() as out:
            q += q2
    assert qp.ccz_count(out) == 18

    # Classes can specify custom behavior via __riadd__.
    class Riadd:
        def __riadd__(self, other):
            qp.phase_flip()
            return other
    with qp.capture() as out:
        q += Riadd()
    assert out == [('phase_flip', qp.QubitIntersection.ALWAYS)]
示例#2
0
def test_len_getitem():
    h = 'test'
    q = qp.Quint(qp.NamedQureg(h, 10))
    assert len(q) == 10

    with pytest.raises(IndexError):
        _ = q[-100]

    assert q[0] == qp.Qubit(h, 0)
    assert q[-1] == qp.Qubit(h, 9)
    assert q[2:4].qureg == qp.Quint(qp.RangeQureg(
        qp.NamedQureg(h, 10), range(2, 4))).qureg
示例#3
0
 def __getitem__(self, item):
     if isinstance(item, bool):
         return self.values[int(item)]
     if isinstance(item, int):
         return self.values[item]
     if isinstance(item, slice):
         return LookupTable(self.values[item])
     if isinstance(item, tuple):
         if all(isinstance(e, qp.Quint) for e in item):
             reg = qp.RawQureg(q for e in item[::-1] for q in e)
             return qp.LookupRValue(self, qp.Quint(reg))
     if isinstance(item, qp.Quint):
         return qp.LookupRValue(self, item)
     if isinstance(item, qp.Qubit):
         return qp.LookupRValue(self, qp.Quint(item.qureg))
     raise NotImplementedError('Strange index: {}'.format(item))
def test_quint_borrowed():
    @qp.semi_quantum
    def f(x: qp.Quint.Borrowed):
        return x

    q = qp.Quint(qp.NamedQureg('a', 10))
    assert f(q) is q

    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            v = f(2)
            assert isinstance(v, qp.Quint)
    cirq.testing.assert_has_diagram(circuit,
                                    """
_f_x[0]: -------alloc-------Mxc--------cxM---release---
                |           |          |     |
_f_x[1]: -------alloc---X---Mxc--------cxM---release---

global phase:                     pi
    """,
                                    use_unicode_characters=False)

    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            v = f(True)
            assert isinstance(v, qp.Quint)
    cirq.testing.assert_has_diagram(circuit,
                                    """
_f_x: ----------alloc---X---Mxc--------cxM---release---

global phase:                     pi
        """,
                                    use_unicode_characters=False)

    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            rval = qp.LookupTable([1, 2, 3])[q]
            v = f(rval)
            assert isinstance(v, qp.Quint)
    cirq.testing.assert_has_diagram(circuit,
                                    """
_f_x[0]: ----------alloc-------------------------------------X------------------------------------------X-----------------------------Mxc---X---@---X-------------------Z-------------------------------------X---------@---------Mxc--------cxM---cxM---release---
                   |                                         |                                          |                             |         |   |                   |                                     |         |                          |     |
_f_x[1]: ----------alloc-------------------------------------|--------X---------------------------------X-----------------------------Mxc-------X---@-------------------|---Z---------------------------------@---Mxc---|---cxM--------------------cxM---release---
                                                             |        |                                 |                                       |                       |   |                                           |
_lookup_prefix: -----------alloc---X---X---alloc---@X---@X---@---@X---@---Mxc---@---cxM---release---X---@---Mxc-------cxM---release-------------|-------alloc---X---X---@---@---X---Mxc-------cxM---release-------------|------------------------------------------
                                   |               |                            |                                                               |               |                                                       |
a[0]: -----------------------------|---------------@----------------------------Z---------------------------------------------------------------@---------------|-------------------------------------------------------Z------------------------------------------
                                   |                                                                                                                            |
a[1]: -----------------------------@------------------------------------------------------------------------------Z---------------------------------------------@-------------------------Z------------------------------------------------------------------------

global phase:                                                                                                                                                                                                                           pi
            """,
                                    use_unicode_characters=False)

    with pytest.raises(TypeError, match='quantum integer expression'):
        _ = f('test')
def test_qubit():
    @qp.semi_quantum
    def f(x: qp.Qubit):
        return x

    q = qp.Qubit('a', 10)
    assert f(q) is q
    with pytest.raises(TypeError, match='Expected a qp.Qubit'):
        _ = f(2)
    with pytest.raises(TypeError, match='Expected a qp.Qubit'):
        _ = f('a')
    with pytest.raises(TypeError, match='Expected a qp.Qubit'):
        _ = f(qp.Quint(qp.NamedQureg('a', 10)))
    with pytest.raises(TypeError, match='Expected a qp.Qubit'):
        _ = f(qp.BoolRValue(True))
def test_multiple():
    @qp.semi_quantum
    def add(target: qp.Quint,
            offset: qp.Quint.Borrowed,
            *,
            control: qp.Qubit.Control = False):
        assert isinstance(target, qp.Quint)
        assert isinstance(offset, qp.Quint)
        assert isinstance(control, qp.QubitIntersection)
        target += offset & qp.controlled_by(control)

    a = qp.Quint(qp.NamedQureg('a', 5))
    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            add(a, 10, control=True)
    assert len(circuit) >= 5
示例#7
0
def test_init():
    q1 = qp.Quint(qp.NamedQureg('test', 10))
    q2 = qp.Quint(qp.NamedQureg('test', 10))
    assert q1.qureg == q2.qureg
    assert str(q1) == str(q2) == 'test'
示例#8
0
def test_ixor():
    q = qp.Quint(qp.NamedQureg('test', 10))

    with pytest.raises(TypeError):
        q ^= None

    with qp.LogCirqCircuit() as circuit:
        q ^= 5
    cirq.testing.assert_has_diagram(circuit, """
test[0]: ---X---
            |
test[2]: ---X---
    """, use_unicode_characters=False)

    q2 = qp.Quint(qp.NamedQureg('test2', 5))
    with qp.LogCirqCircuit() as circuit:
        q ^= q2
        cirq.testing.assert_has_diagram(circuit, """
test2[0]: ---@-------------------
             |
test2[1]: ---|---@---------------
             |   |
test2[2]: ---|---|---@-----------
             |   |   |
test2[3]: ---|---|---|---@-------
             |   |   |   |
test2[4]: ---|---|---|---|---@---
             |   |   |   |   |
test[0]: ----X---|---|---|---|---
                 |   |   |   |
test[1]: --------X---|---|---|---
                     |   |   |
test[2]: ------------X---|---|---
                         |   |
test[3]: ----------------X---|---
                             |
test[4]: --------------------X---
        """, use_unicode_characters=False)

    q3 = qp.Quint(qp.NamedQureg('test3', 5))
    c = qp.Qubit('c')
    with qp.LogCirqCircuit() as circuit:
        q ^= q3 & qp.controlled_by(c)
        cirq.testing.assert_has_diagram(circuit, """
c: ----------@---@---@---@---@---
             |   |   |   |   |
test3[0]: ---@---|---|---|---|---
             |   |   |   |   |
test3[1]: ---|---@---|---|---|---
             |   |   |   |   |
test3[2]: ---|---|---@---|---|---
             |   |   |   |   |
test3[3]: ---|---|---|---@---|---
             |   |   |   |   |
test3[4]: ---|---|---|---|---@---
             |   |   |   |   |
test[0]: ----X---|---|---|---|---
                 |   |   |   |
test[1]: --------X---|---|---|---
                     |   |   |
test[2]: ------------X---|---|---
                         |   |
test[3]: ----------------X---|---
                             |
test[4]: --------------------X---
            """, use_unicode_characters=False)

    # Classes can specify custom behavior via __rixor__.
    class Rixor:
        def __rixor__(self, other):
            qp.phase_flip()
            return other
    with qp.capture() as out:
        q ^= Rixor()
    assert out == [('phase_flip', qp.QubitIntersection.ALWAYS)]
示例#9
0
def test_mul_rmul():
    q = qp.Quint(qp.NamedQureg('test', 10))

    assert q * 5 == 5 * q == qp.ScaledIntRValue(q, 5)
示例#10
0
def test_set_item_blocks():
    q = qp.Quint(qp.NamedQureg('test', 10))

    with pytest.raises(NotImplementedError):
        q[2] = qp.Qubit()
def test_qubit_control():
    @qp.semi_quantum
    def f(x: qp.Qubit.Control):
        return x

    q = qp.Qubit('a', 10)
    q2 = qp.Qubit('b', 8)

    # Note: The lack of capture context means we are implicitly asserting the following invokations perform no
    # quantum operations such as allocating a qubit.

    # Definitely false.
    assert f(False) == qp.QubitIntersection.NEVER
    assert f(qp.QubitIntersection.NEVER) == qp.QubitIntersection.NEVER

    # Definitely true.
    assert f(qp.QubitIntersection.ALWAYS) == qp.QubitIntersection.ALWAYS
    assert f(None) == qp.QubitIntersection.ALWAYS
    assert f(True) == qp.QubitIntersection.ALWAYS

    # Single qubit.
    assert f(q) == qp.QubitIntersection((q, ))
    assert f(qp.QubitIntersection((q, ))) == qp.QubitIntersection((q, ))

    # Multi qubit intersection.
    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            v = f(q & q2)
            assert isinstance(v, qp.QubitIntersection)
            del v
    cirq.testing.assert_has_diagram(circuit,
                                    """
_f_x: ----alloc---X---Mxc-------cxM---release---
                  |
a[10]: -----------@---------@-------------------
                  |         |
b[8]: ------------@---------Z-------------------
        """,
                                    use_unicode_characters=False)

    # Arbitrary expression
    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            rval = qp.Quint(qp.NamedQureg('a', 2)) > qp.Quint(
                qp.NamedQureg('b', 2))
            v = f(rval)
            assert isinstance(v, qp.QubitIntersection)
            q = v.qubits[0]
            assert q.name == '_f_x'
            del q
            del v
    cirq.testing.assert_has_diagram(circuit,
                                    """
_do_if_less_than_or_equal: -----------alloc---@---X---@-------------------------------@---X---@---Mxc---cxM---release---------alloc---@---X---@-------------------------------@---X---@---Mxc---cxM---release-------------------
                                              |   |   |                               |   |   |                                       |   |   |                               |   |   |
_f_x: ------------------------alloc-----------|---|---|---------------X---------------|---|---|-------------------------Mxc-----------|---|---|-------------------------------|---|---|-------------------------cxM---release---
                                              |   |   |               |               |   |   |                                       |   |   |                               |   |   |
a[0]: ----------------------------------------|---@---X---@---X---@---|---@---X---@---X---@---|---------------------------------------|---@---X---@---X---@-------@---X---@---X---@---|-----------------------------------------
                                              |       |   |   |   |   |   |   |   |   |       |                                       |       |   |   |   |       |   |   |   |       |
a[1]: ----------------------------------------|-------|---|---@---X---@---X---@---|---|-------|---------------------------------------|-------|---|---@---X---Z---X---@---|---|-------|-----------------------------------------
                                              |       |   |       |       |       |   |       |                                       |       |   |       |       |       |   |       |
b[0]: ----------------------------------------X-------@---|-------|-------|-------|---@-------X---------------------------------------X-------@---|-------|-------|-------|---@-------X-----------------------------------------
                                                          |       |       |       |                                                               |       |       |       |
b[1]: ----------------------------------------------------X-------@-------@-------X---------------------------------------------------------------X-------@-------@-------X-----------------------------------------------------
        """,
                                    use_unicode_characters=False)

    with pytest.raises(TypeError, match='quantum control expression'):
        _ = f('test')
    with pytest.raises(TypeError, match='quantum control expression'):
        _ = f(qp.Quint(qp.NamedQureg('a', 10)))
    with pytest.raises(TypeError, match='quantum control expression'):
        _ = f(qp.Quint(qp.NamedQureg('a', 10)))
示例#12
0
 def do_measure(self, qureg: 'qp.Qureg', reset: bool) -> int:
     reg = self.quint_buf(qp.Quint(qureg))
     result = int(reg)
     if reset:
         reg[:] = 0
     return result
示例#13
0
 def resolve(self, sim_state: 'qp.ClassicalSimState', allow_mutate: bool):
     if not allow_mutate:
         return [q.resolve(sim_state, False) for q in self]
     return sim_state.quint_buf(qp.Quint(self))
示例#14
0
 def resolve(self, sim_state: 'qp.ClassicalSimState', allow_mutate: bool):
     buf = sim_state.quint_buf(qp.Quint(self.qureg))
     return buf if allow_mutate else bool(int(buf))
示例#15
0
 def __getitem__(self, item):
     if isinstance(item, int):
         return self.qureg[item]
     return qp.Quint(self.qureg[item])
示例#16
0
 def resolve(self, sim_state: 'qp.ClassicalSimState', allow_mutate: bool) -> bool:
     v = qp.Quint(qp.RawQureg(self.qubits)).resolve(sim_state, False)
     return self.bit and v == (1 << len(self.qubits)) - 1
def test_qubit_borrowed():
    @qp.semi_quantum
    def f(x: qp.Qubit.Borrowed):
        return x

    q = qp.Qubit('a', 10)
    assert f(q) is q

    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            v = f(True)
            assert isinstance(v, qp.Qubit)
            del v
    cirq.testing.assert_has_diagram(circuit,
                                    """
_f_x: ----------alloc---X---Mxc--------cxM---release---

global phase:                     pi
                """,
                                    use_unicode_characters=False)

    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            v = f(0)
            assert isinstance(v, qp.Qubit)
            del v
    cirq.testing.assert_has_diagram(circuit,
                                    """
_f_x: ---alloc---Mxc---cxM---release---
                """,
                                    use_unicode_characters=False)

    with qp.RandomSim(measure_bias=1):
        with qp.LogCirqCircuit() as circuit:
            rval = qp.Quint(qp.NamedQureg('a', 3)) > qp.Quint(
                qp.NamedQureg('b', 3))
            v = f(rval)
            assert isinstance(v, qp.Qubit)
            del v
    cirq.testing.assert_has_diagram(circuit,
                                    """
_do_if_less_than_or_equal: -----------alloc---@---X---@-------------------------------------------------------@---X---@---Mxc---cxM---release---------alloc---@---X---@-------------------------------------------------------@---X---@---Mxc---cxM---release-------------------
                                              |   |   |                                                       |   |   |                                       |   |   |                                                       |   |   |
_f_x: ------------------------alloc-----------|---|---|---------------------------X---------------------------|---|---|-------------------------Mxc-----------|---|---|-------------------------------------------------------|---|---|-------------------------cxM---release---
                                              |   |   |                           |                           |   |   |                                       |   |   |                                                       |   |   |
a[0]: ----------------------------------------|---@---X---@---X---@---------------|---------------@---X---@---X---@---|---------------------------------------|---@---X---@---X---@-------------------------------@---X---@---X---@---|-----------------------------------------
                                              |       |   |   |   |               |               |   |   |   |       |                                       |       |   |   |   |                               |   |   |   |       |
a[1]: ----------------------------------------|-------|---|---@---X---@---X---@---|---@---X---@---X---@---|---|-------|---------------------------------------|-------|---|---@---X---@---X---@-------@---X---@---X---@---|---|-------|-----------------------------------------
                                              |       |   |       |   |   |   |   |   |   |   |   |       |   |       |                                       |       |   |       |   |   |   |       |   |   |   |       |   |       |
a[2]: ----------------------------------------|-------|---|-------|---|---@---X---@---X---@---|---|-------|---|-------|---------------------------------------|-------|---|-------|---|---@---X---Z---X---@---|---|-------|---|-------|-----------------------------------------
                                              |       |   |       |   |       |       |       |   |       |   |       |                                       |       |   |       |   |       |       |       |   |       |   |       |
b[0]: ----------------------------------------X-------@---|-------|---|-------|-------|-------|---|-------|---@-------X---------------------------------------X-------@---|-------|---|-------|-------|-------|---|-------|---@-------X-----------------------------------------
                                                          |       |   |       |       |       |   |       |                                                               |       |   |       |       |       |   |       |
b[1]: ----------------------------------------------------X-------@---|-------|-------|-------|---@-------X---------------------------------------------------------------X-------@---|-------|-------|-------|---@-------X-----------------------------------------------------
                                                                      |       |       |       |                                                                                       |       |       |       |
b[2]: ----------------------------------------------------------------X-------@-------@-------X---------------------------------------------------------------------------------------X-------@-------@-------X-----------------------------------------------------------------
        """,
                                    use_unicode_characters=False)

    with pytest.raises(TypeError, match='quantum boolean expression'):
        _ = f('test')