def test_del_and_circuit():
    with qp.Sim(enforce_release_at_zero=False, phase_fixup_bias=True):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='q') as q:
                q[0].clear(q[1] & q[2])

    cirq.testing.assert_has_diagram(circuit, r"""
q[0]: ---alloc---Mxc-------cxM---release---
         |                       |
q[1]: ---alloc---------@---------release---
         |             |         |
q[2]: ---alloc---------Z---------release---
        """, use_unicode_characters=False)

    with qp.Sim(enforce_release_at_zero=False, phase_fixup_bias=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='b') as q:
                q[0].clear(q[1] & q[2])

    cirq.testing.assert_has_diagram(circuit, r"""
b[0]: ---alloc---Mxc---cxM---release---
         |                   |
b[1]: ---alloc---------------release---
         |                   |
b[2]: ---alloc---------------release---
        """, use_unicode_characters=False)
Beispiel #2
0
def test_del_unary_circuit():
    with qp.Sim(phase_fixup_bias=True):
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            (1 << b).clear_storage_location(u, c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -------------------------------------------------------------------------------------------------------------------------------------------------------Z---------

b[0]: -------------------------------------------------------------------------------------------------------------------------------------Z-------------------------
                                                                                                                                           |
b[1]: ---------------------------------------------------------------------------------------------Z-------------------Z-------------------|-------------------------
                                                                                                   |                   |                   |
b[2]: -------------Z-------------------Z-------------------Z-------------------Z-------------------|-------------------|-------------------|-------------------------
                   |                   |                   |                   |                   |                   |                   |
u[0]: -------------|-------------------|-------------------|---------X---------@-------------------|---------X---------@---------X---------@---------Mxc-------cxM---
                   |                   |                   |         |                             |         |                   |
u[1]: -------------|-------------------|---------X---------@---------|-------------------X---------@---------|-------------------@---Mxc-------cxM-------------------
                   |                   |         |                   |                   |                   |
u[2]: -------------|---------X---------@---------|-------------------|-------------------|-------------------@---Mxc-------cxM---------------------------------------
                   |         |                   |                   |                   |
u[3]: ---X---------@---------|-------------------|-------------------|-------------------@---Mxc-------cxM-----------------------------------------------------------
         |                   |                   |                   |
u[4]: ---|-------------------|-------------------|-------------------@---Mxc-------cxM-------------------------------------------------------------------------------
         |                   |                   |
u[5]: ---|-------------------|-------------------@---Mxc-------cxM---------------------------------------------------------------------------------------------------
         |                   |
u[6]: ---|-------------------@---Mxc-------cxM-----------------------------------------------------------------------------------------------------------------------
         |
u[7]: ---@---Mxc-------cxM-------------------------------------------------------------------------------------------------------------------------------------------
""", use_unicode_characters=False)

    with qp.Sim(phase_fixup_bias=False):
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            (1 << b).clear_storage_location(u, c)

    cirq.testing.assert_has_diagram(circuit, r"""
u[0]: ---------------------------------------------------X-------------------------------X---------------X---------------Mxc---cxM---
                                                         |                               |               |
u[1]: -----------------------------------X---------------|---------------X---------------|---------------@---Mxc---cxM---------------
                                         |               |               |               |
u[2]: -------------------X---------------|---------------|---------------|---------------@---Mxc---cxM-------------------------------
                         |               |               |               |
u[3]: ---X---------------|---------------|---------------|---------------@---Mxc---cxM-----------------------------------------------
         |               |               |               |
u[4]: ---|---------------|---------------|---------------@---Mxc---cxM---------------------------------------------------------------
         |               |               |
u[5]: ---|---------------|---------------@---Mxc---cxM-------------------------------------------------------------------------------
         |               |
u[6]: ---|---------------@---Mxc---cxM-----------------------------------------------------------------------------------------------
         |
u[7]: ---@---Mxc---cxM---------------------------------------------------------------------------------------------------------------
        """, use_unicode_characters=False)
Beispiel #3
0
def test_ixor():
    q = qp.Qubit('q')
    c = qp.Qubit('c')
    d = qp.Qubit('d')

    # Unsupported classes cause type error.
    with pytest.raises(TypeError):
        q ^= None

    class C:
        pass

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

    # False does nothing. True causes toggle.
    with qp.LogCirqCircuit() as circuit:
        q ^= False
    assert len(circuit) == 0
    with qp.LogCirqCircuit() as circuit:
        q ^= True
    cirq.testing.assert_has_diagram(circuit,
                                    """
q: ---X---    
    """,
                                    use_unicode_characters=False)

    # Qubit and qubit intersection cause controlled toggle.
    with qp.LogCirqCircuit() as circuit:
        q ^= c
    cirq.testing.assert_has_diagram(circuit,
                                    """
c: ---@---
      |
q: ---X---    
        """,
                                    use_unicode_characters=False)
    with qp.LogCirqCircuit() as circuit:
        q ^= c & d
    cirq.testing.assert_has_diagram(circuit,
                                    """
c: ---@---
      |
d: ---@---
      |
q: ---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)]
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_plus_equal_gate_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        a = qp.qalloc(len=3, name='a')
        t = qp.qalloc(len=4, name='t')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            qp.arithmetic.do_addition(lvalue=t, offset=a, carry_in=c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -----X-------@---------------------------------------------------------------@---@-------X---
         |       |                                                               |   |       |
a[0]: ---@---@---X---X-------@-----------------------------------@---@-------X---X---|---@---@---
             |   |   |       |                                   |   |       |   |   |   |
a[1]: -------|---|---@---@---X---X-------@-------@---@-------X---X---|---@---@---|---|---|-------
             |   |       |   |   |       |       |   |       |   |   |   |       |   |   |
a[2]: -------|---|-------|---|---@---@---X---@---X---|---@---@---|---|---|-------|---|---|-------
             |   |       |   |       |   |   |   |   |   |       |   |   |       |   |   |
t[0]: -------X---@-------|---|-------|---|---|---|---|---|-------|---|---|-------@---X---X-------
                         |   |       |   |   |   |   |   |       |   |   |
t[1]: -------------------X---@-------|---|---|---|---|---|-------@---X---X-----------------------
                                     |   |   |   |   |   |
t[2]: -------------------------------X---@---|---@---X---X---------------------------------------
                                             |
t[3]: ---------------------------------------X---------------------------------------------------
        """, use_unicode_characters=False)
Beispiel #6
0
def test_let_unary_circuit():
    with qp.Sim():
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            (1 << b).init_storage_location(u, c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -----@-----------------------------------------------------------
         |
b[0]: ---|---@-------------------------------------------------------
         |   |
b[1]: ---|---|-------@-------@---------------------------------------
         |   |       |       |
b[2]: ---|---|-------|-------|-------@-------@-------@-------@-------
         |   |       |       |       |       |       |       |
u[0]: ---X---@---X---@---X---|-------@---X---|-------|-------|-------
             |   |   |   |   |       |   |   |       |       |
u[1]: -------X---@---|---|---@---X---|---|---@---X---|-------|-------
                     |   |   |   |   |   |   |   |   |       |
u[2]: ---------------X---@---|---|---|---|---|---|---@---X---|-------
                             |   |   |   |   |   |   |   |   |
u[3]: -----------------------X---@---|---|---|---|---|---|---@---X---
                                     |   |   |   |   |   |   |   |
u[4]: -------------------------------X---@---|---|---|---|---|---|---
                                             |   |   |   |   |   |
u[5]: ---------------------------------------X---@---|---|---|---|---
                                                     |   |   |   |
u[6]: -----------------------------------------------X---@---|---|---
                                                             |   |
u[7]: -------------------------------------------------------X---@---
        """, use_unicode_characters=False)
def test_xor_equal_gate_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='a') as a:
                with qp.qalloc(len=4, name='t') as t:
                    with qp.qalloc(name='_c') as c:
                        qp.arithmetic.do_xor(lvalue=t, mask=a)
                        qp.arithmetic.do_xor(lvalue=t, mask=a, control=c)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_c: ---------------------alloc---------------@---@---@---release-----------------------
                                             |   |   |
a[0]: ---alloc-------------------@-----------@---|---|-----------------------release---
         |                       |           |   |   |                       |
a[1]: ---alloc-------------------|---@-------|---@---|-----------------------release---
         |                       |   |       |   |   |                       |
a[2]: ---alloc-------------------|---|---@---|---|---@-----------------------release---
                                 |   |   |   |   |   |
t[0]: -----------alloc-----------X---|---|---X---|---|-------------release-------------
                 |                   |   |       |   |             |
t[1]: -----------alloc---------------X---|-------X---|-------------release-------------
                 |                       |           |             |
t[2]: -----------alloc-------------------X-----------X-------------release-------------
                 |                                                 |
t[3]: -----------alloc---------------------------------------------release-------------
        """,
                                    use_unicode_characters=False)
def test_xor_equal_gate_circuit_2():
    with qp.Sim(enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='a') as a:
                with qp.qalloc(len=4, name='t') as t:
                    with qp.qalloc(name='_c') as c:
                        t ^= a
                        t ^= a & qp.controlled_by(c)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_c: ---------------------alloc---------------@---@---@---release-----------------------
                                             |   |   |
a[0]: ---alloc-------------------@-----------@---|---|-----------------------release---
         |                       |           |   |   |                       |
a[1]: ---alloc-------------------|---@-------|---@---|-----------------------release---
         |                       |   |       |   |   |                       |
a[2]: ---alloc-------------------|---|---@---|---|---@-----------------------release---
                                 |   |   |   |   |   |
t[0]: -----------alloc-----------X---|---|---X---|---|-------------release-------------
                 |                   |   |       |   |             |
t[1]: -----------alloc---------------X---|-------X---|-------------release-------------
                 |                       |           |             |
t[2]: -----------alloc-------------------X-----------X-------------release-------------
                 |                                                 |
t[3]: -----------alloc---------------------------------------------release-------------
        """,
                                    use_unicode_characters=False)
def test_if_less_than_then_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        lhs = qp.qalloc(len=4, name='lhs')
        rhs = qp.qalloc(len=4, name='rhs')
        c = qp.qalloc(name='_or_eq')
        t = qp.qalloc(name='t')
        with qp.LogCirqCircuit() as circuit:
            qp.arithmetic.do_if_less_than(
                lhs=lhs,
                rhs=rhs,
                or_equal=c,
                effect=t.__ixor__)
    cirq.testing.assert_has_diagram(circuit, r"""
_or_eq: ---@---X---@-------------------------------------------------------------------------------@---X---@---
           |   |   |                                                                               |   |   |
lhs[0]: ---X---|---@-------------------------------------------------------------------------------@---|---X---
               |   |                                                                               |   |
lhs[1]: -------|---|---X-------@-------------------------------------------------------@-------X---|---|-------
               |   |   |       |                                                       |       |   |   |
lhs[2]: -------|---|---|-------|---X-------@-------------------------------@-------X---|-------|---|---|-------
               |   |   |       |   |       |                               |       |   |       |   |   |
lhs[3]: -------|---|---|-------|---|-------|---X-------@-------@-------X---|-------|---|-------|---|---|-------
               |   |   |       |   |       |   |       |       |       |   |       |   |       |   |   |
rhs[0]: -------@---X---@---X---@---|-------|---|-------|-------|-------|---|-------|---@---X---@---X---@-------
                           |   |   |       |   |       |       |       |   |       |   |   |
rhs[1]: -------------------@---X---@---X---@---|-------|-------|-------|---@---X---@---X---@-------------------
                                       |   |   |       |       |       |   |   |
rhs[2]: -------------------------------@---X---@---X---@-------@---X---@---X---@-------------------------------
                                                   |   |       |   |
rhs[3]: -------------------------------------------@---X---@---X---@-------------------------------------------
                                                           |
t: --------------------------------------------------------X---------------------------------------------------
        """, use_unicode_characters=False)
def test_init_square_circuit():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        factor = qp.qalloc(len=2, name='f')
        out = qp.qalloc(len=4, name='s')
        with qp.LogCirqCircuit() as circuit:
            init_square(factor=factor, clean_out=out)
    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_do_addition_carry_in: -----------------------alloc---X-------@---------------------------------------------------------------@---@-------X---Mxc---cxM---release-----------------alloc---X-------@-------------------------------@---@-------X---Mxc---cxM---release-------------
                                                      |       |                                                               |   |       |                                               |       |                               |   |       |
_sqr_offset: ---------------------alloc---X-----------|-------|---------------@---@---X---@---X-------@---@-------------------|---|-------|-------------------------X---release-----------|-------|-------------------------------|---|-------|-----------------------------------
                                          |           |       |               |   |   |   |   |       |   |                   |   |       |                         |                     |       |                               |   |       |
_sqr_zero: ---------------alloc-----------|-----------|-------|---@---@---X---X---|---@---|---@---@---|---X---X-------@---@---|---|-------|-------------------------|---------------------|-------|---@---@---X---X-------@---@---|---|-------|-------------------------release---
                                          |           |       |   |   |   |       |   |   |   |   |   |       |       |   |   |   |       |                         |                     |       |   |   |   |   |       |   |   |   |       |
f[0]: ------------------------------------@-----------@---@---X---X---|---@-------|---|---|---|---|---|-------@---@---|---X---X---|---@---@-------------------------@---------------------|-------|---|---|---|---|-------|---|---|---|-------|-----------------------------------
                                          |               |   |       |   |       |   |   |   |   |   |       |   |   |       |   |   |                             |                     |       |   |   |   |   |       |   |   |   |       |
f[1]: ------------------------------------@---------------|---|-------|---|-------|---|---|---|---|---|-------|---|---|-------|---|---|-----------------------------@---------------------@---@---X---X---|---@---@---@---|---X---X---|---@---@-----------------------------------
                                                          |   |       |   |       |   |   |   |   |   |       |   |   |       |   |   |                                                       |   |       |   |   |   |   |       |   |   |
s[0]: ----------------------------------------------------X---@-------|---|-------|---|---|---|---|---|-------|---|---|-------@---X---X-------------------------------------------------------|---|-------|---|---|---|---|-------|---|---|---------------------------------------
                                                                      |   |       |   |   |   |   |   |       |   |   |                                                                       |   |       |   |   |   |   |       |   |   |
s[1]: ----------------------------------------------------------------X---@-------|---|---|---|---|---|-------@---X---X-----------------------------------------------------------------------|---|-------|---|---|---|---|-------|---|---|---------------------------------------
                                                                                  |   |   |   |   |   |                                                                                       |   |       |   |   |   |   |       |   |   |
s[2]: ----------------------------------------------------------------------------X---@---|---@---X---X---------------------------------------------------------------------------------------X---@-------|---|---|---|---|-------@---X---X---------------------------------------
                                                                                          |                                                                                                               |   |   |   |   |
s[3]: ------------------------------------------------------------------------------------X---------------------------------------------------------------------------------------------------------------X---@---@---X---X-------------------------------------------------------
""",
                                    use_unicode_characters=False)
def test_let_and_circuit():
    with qp.Sim(enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=3, name='q') as q:
                q[0].init(q[1] & q[2])

    cirq.testing.assert_has_diagram(circuit, r"""
q[0]: ---alloc---X---release---
         |       |   |
q[1]: ---alloc---@---release---
         |       |   |
q[2]: ---alloc---@---release---
        """, use_unicode_characters=False)
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
Beispiel #13
0
def test_xor_lookup():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='addr') as addr:
                with qp.qalloc(len=8, name='out') as out:
                    with qp.qalloc(name='cnt') as cnt:
                        out ^= qp.LookupTable(range(
                            1, 17))[addr] & qp.controlled_by(cnt)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_lookup_prefix: -----------------------------alloc---X---X-----------@---@-------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------X-----------@---@-------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------Mxc-------cxM---release---------------------------------
                                                     |   |           |   |                                                                                                                                                             |                                                                                                                                                                   |                   |           |   |                                                                                                                                                             |                                                                                                                                                                   |
_lookup_prefix_1: -----------------------------------|---|---alloc---X---X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------Mxc---|---cxM---release---|---alloc---X---X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------Mxc---|---cxM---release-----------------------------------------------------------
                                                     |   |           |               |   |                                                         |                                                               |                               |   |                                                         |                                                               |                         |                   |           |               |   |                                                         |                                                               |                               |   |                                                         |                                                               |                         |
_lookup_prefix_2: -----------------------------------|---|-----------|-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release---------|-------------------|-----------|-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release---------|---------------------------------------------------------------------------
                                                     |   |           |               |               |   |       |             |                               |   |       |             |                         |                               |               |   |       |             |                               |   |       |             |                         |                         |                   |           |               |               |   |       |             |                               |   |       |             |                         |                               |               |   |       |             |                               |   |       |             |                         |                         |
_lookup_prefix_3: -----------------------------------|---|-----------|---------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------|-------------------|-----------|---------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------|---------------------------------------------------------------------------
                                                     |   |           |               |               |       |       |         |                               |       |       |         |                         |                               |               |       |       |         |                               |       |       |         |                         |                         |                   |           |               |               |       |       |         |                               |       |       |         |                         |                               |               |       |       |         |                               |       |       |         |                         |                         |
addr[0]: ------------alloc---------------------------|---|-----------|---------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------|-------------------|-----------|---------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------|-----------------------------------------------------------------release---
                     |                               |   |           |               |                       |       |                                                 |       |                                   |                               |                       |       |                                                 |       |                                   |                         |                   |           |               |                       |       |                                                 |       |                                   |                               |                       |       |                                                 |       |                                   |                         |                                                                 |
addr[1]: ------------alloc---------------------------|---|-----------|---------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------|-------------------|-----------|---------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------|-----------------------------------------------------------------release---
                     |                               |   |           |                                       |       |                                                 |       |                                                                                           |       |                                                 |       |                                                             |                   |           |                                       |       |                                                 |       |                                                                                           |       |                                                 |       |                                                             |                                                                 |
addr[2]: ------------alloc---------------------------|---|-----------@---------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------Z-------------------|-----------@---------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------Z-----------------------------------------------------------------release---
                     |                               |   |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                 |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                               |
addr[3]: ------------alloc---------------------------@---|---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------|---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------------Z---------------------------------------release---
                                                     |   |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                 |                                                   |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                       |
cnt: --------------------------------alloc-----------@---@---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------@---------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------------@-------------------release-----------------------
                                                                                                             |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |
out[0]: ---------------------alloc---------------------------------------------------------------------------X-------|-------------------------------------------------X-------|-------------------------------------------------------------------------------------------X-------|-------------------------------------------------X-------|-------------------------------------------------------------------------------------------------------------------------------------X-------|-------------------------------------------------X-------|-------------------------------------------------------------------------------------------X-------|-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                     |
out[1]: ---------------------alloc-----------------------------------------------------------------------------------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------|-------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------------------------------------------------|-------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------|-------X-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                 |                                                                                           |       |                                                 |       |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                     |
out[2]: ---------------------alloc---------------------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------|-------------------------------------------------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------X-------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                               |                                                                                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |                                                                                                                     |
out[3]: ---------------------alloc-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------X-------------------------------------------------------------------------------------------X-------X-------------------------------------------------X-------|---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |                                                                                                                     |
out[4]: ---------------------alloc---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X---------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
out[5]: ---------------------alloc-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
out[6]: ---------------------alloc-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------release-------------
                             |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
out[7]: ---------------------alloc-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------release-------------
        """,
                                    use_unicode_characters=False)
def test_eq_circuit():
    with qp.Sim(enforce_release_at_zero=False, phase_fixup_bias=True):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='lhs') as lhs:
                with qp.hold(lhs == 5, name='target'):
                    pass

    cirq.testing.assert_has_diagram(circuit, r"""
lhs[0]: ---alloc---------------@-----------------@-----------------------release---
           |                   |                 |                       |
lhs[1]: ---alloc-----------X---@---X---------X---@---X-------------------release---
           |               |   |   |         |   |   |                   |
lhs[2]: ---alloc-----------|---@---|---------|---@---|-------------------release---
           |               |   |   |         |   |   |                   |
lhs[3]: ---alloc-----------X---@---X---------X---Z---X-------------------release---
                               |
target: -----------alloc-------X-------Mxc---------------cxM---release-------------
        """, use_unicode_characters=False)
Beispiel #15
0
def test_xor_unary_circuit():
    with qp.Sim(phase_fixup_bias=True):
        b = qp.qalloc(len=3, name='b')
        u = qp.qalloc(len=8, name='u')
        c = qp.qalloc(name='_c')
        with qp.LogCirqCircuit() as circuit:
            u ^= (1 << b) & qp.controlled_by(c)

    cirq.testing.assert_has_diagram(circuit, r"""
_c: -------------------------@---@-------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------------------------------------------------------------------------------------------------------------------------------------------------------@-------------------
                             |   |                                                                                                                                                             |                                                                                                                                                                   |
_lookup_prefix: -----alloc---X---X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------X-----------@---@---------------------------------------------------------@---------------------------------------------------------------@-------------------Mxc---|---cxM---release---
                             |               |   |                                                         |                                                               |                               |   |                                                         |                                                               |                         |
_lookup_prefix_1: -----------|-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release-------alloc---X---X-----------@---@-------@-------------@-------------------X-----------@---@-------@-------------@-------------------Mxc---|---cxM---release---------|-------------------
                             |               |               |   |       |             |                               |   |       |             |                         |                               |               |   |       |             |                               |   |       |             |                         |                         |
_lookup_prefix_2: -----------|---------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------------|-------alloc---X---X---@---X---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---Mxc---|---cxM---release---------|-------------------------|-------------------
                             |               |               |       |       |         |                               |       |       |         |                         |                               |               |       |       |         |                               |       |       |         |                         |                         |
b[0]: -----------------------|---------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------------|---------------@-------|-------|---------Z-------------------------------@-------|-------|---------Z-------------------------|-------------------------|-------------------
                             |               |                       |       |                                                 |       |                                   |                               |                       |       |                                                 |       |                                   |                         |
b[1]: -----------------------|---------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------------@-----------------------|-------|-------------------------------------------------|-------|-----------------------------------Z-------------------------|-------------------
                             |                                       |       |                                                 |       |                                                                                           |       |                                                 |       |                                                             |
b[2]: -----------------------@---------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|-------------------------------------------------------------Z-------------------
                                                                     |       |                                                 |       |                                                                                           |       |                                                 |       |
u[0]: ---------------------------------------------------------------X-------|-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                             |                                                 |       |                                                                                           |       |                                                 |       |
u[1]: -----------------------------------------------------------------------X-------------------------------------------------|-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                               |       |                                                                                           |       |                                                 |       |
u[2]: -------------------------------------------------------------------------------------------------------------------------X-------|-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                       |                                                                                           |       |                                                 |       |
u[3]: ---------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------------------------------------------------|-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                   |       |                                                 |       |
u[4]: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------|-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                           |                                                 |       |
u[5]: -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------------------------------------------------|-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                                                                             |       |
u[6]: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X-------|---------------------------------------------------------------------------------
                                                                                                                                                                                                                                                                                                     |
u[7]: -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------X---------------------------------------------------------------------------------
        """, use_unicode_characters=False)
Beispiel #16
0
def test_del_lookup():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='addr') as addr:
                with qp.qalloc(name='cnt'):
                    with qp.hold(qp.LookupTable(range(1, 17))[addr],
                                 name='out'):
                        circuit[:] = []

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
_lookup_prefix: ---------------------------------------alloc---X---X-----------@---@---------------@-----------------@-------------------X-----------@---@-------@---------------------@-------------------Mxc-------cxM---release--------------------------------------------------------------------------------------------------------------------
                                                               |               |   |               |                 |                               |   |       |                     |
_lookup_prefix_1: ---------------------------------------------|-------alloc---X---X---@---@---@---X---@---@---Mxc---|---cxM---release-------alloc---X---X---@---X---@---@---@---Mxc---|---cxM---release----------------------------------------------------------------------------------------------------------------------------------------------
                                                               |               |       |   |   |       |   |         |                               |       |       |   |   |         |
addr[0]: ----------------------@-------------------------------|---------------|-------|---|---|-------|---|---------|-------------------------------|-------|-------|---|---|---------|-----------------------------------------------------------------------------------------------Z----------------------------------------------------release---
                               |                               |               |       |   |   |       |   |         |                               |       |       |   |   |         |                                                                                               |                                                    |
addr[1]: ----------------------|-------@-------@---------------|---------------|-------|---|---|-------|---|---------|-------------------------------|-------|-------|---|---|---------|-------------------------------------------------------Z-------------------Z-------------------|----------------------------------------------------release---
                               |       |       |               |               |       |   |   |       |   |         |                               |       |       |   |   |         |                                                       |                   |                   |                                                    |
addr[2]: ----------------------|-------|-------|---------------|---------------@-------|---|---|-------|---|---------Z-------------------------------@-------|-------|---|---|---------Z-------------------------------------------------------|-------------------|-------------------|----------------------------------------------------release---
                               |       |       |               |                       |   |   |       |   |                                                 |       |   |   |                                                                 |                   |                   |                                                    |
addr[3]: ----------------------|-------|-------|---------------@-----------------------|---|---|-------|---|-------------------------------------------------|-------|---|---|-----------------------------------Z-----------------------------|-------------------|-------------------|----------------------------------------------------release---
                               |       |       |                                       |   |   |       |   |                                                 |       |   |   |                                                                 |                   |                   |
cnt: --------------------------|-------|-------|---------------------------------------|---|---|-------|---|-------------------------------------------------|-------|---|---|-----------------------------------------------------------------|-------------------|-------------------|------------------------------------------release-------------
                               |       |       |                                       |   |   |       |   |                                                 |       |   |   |                                                                 |                   |                   |
out[0]: -------------Mxc---X---@---X---@---X---|---------------------------------------Z---|---|-------|---|-------------------------------------------------|-------Z---|---|-----------------------------------------------------------------|---------X---------@---------X---------@---------Mxc--------cxM---cxM---release-----------------------
                     |         |   |   |   |   |                                           |   |       |   |                                                 |           |   |                                                                 |         |                   |                                    |     |
out[1]: -------------Mxc-------X---@---|---|---@---X---------------------------------------Z---|-------|---|-------------------------------------------------|-----------Z---|-------------------------------------------------------X---------@---------|-------------------@---Mxc-------cxM--------------------cxM---release-----------------------
                     |                 |   |   |   |                                           |       |   |                                                 |               |                                                       |                   |                                                        |     |
out[2]: -------------Mxc---------------X---@---|---|-------------------------------------------|-------Z---|-------------------------------------------------Z---------------|-------------------------------------------------------|-------------------@---Mxc-------cxM----------------------------------------cxM---release-----------------------
                     |                         |   |                                           |           |                                                                 |                                                       |                                                                            |     |
out[3]: -------------Mxc-----------------------X---@-------------------------------------------Z-----------Z-----------------------------------------------------------------Z-------------------------------------------------------@---Mxc-------cxM------------------------------------------------------------cxM---release-----------------------
                     |                                                                                                                                                                                                                                                                                            |     |
out[4]: -------------Mxc------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------cxM---release-----------------------

global phase:                                                                                                                                                                                                                                                                                          pi
        """,
                                    use_unicode_characters=False)
Beispiel #17
0
def test_redundant_lookup():
    with qp.Sim(phase_fixup_bias=True, enforce_release_at_zero=False):
        with qp.LogCirqCircuit() as circuit:
            with qp.qalloc(len=4, name='addr') as addr:
                with qp.qalloc(len=8, name='out') as out:
                    with qp.qalloc(name='cnt') as cnt:
                        out ^= qp.LookupTable(
                            [3] * 16)[addr] & qp.controlled_by(cnt)

    cirq.testing.assert_has_diagram(circuit,
                                    r"""
addr[0]: ---alloc-------------------------------------------release---
            |                                               |
addr[1]: ---alloc-------------------------------------------release---
            |                                               |
addr[2]: ---alloc-------------------------------------------release---
            |                                               |
addr[3]: ---alloc-------------------------------------------release---

cnt: -----------------------alloc---@---release-----------------------
                                    |
out[0]: ------------alloc-----------X-------------release-------------
                    |               |             |
out[1]: ------------alloc-----------X-------------release-------------
                    |                             |
out[2]: ------------alloc-------------------------release-------------
                    |                             |
out[3]: ------------alloc-------------------------release-------------
                    |                             |
out[4]: ------------alloc-------------------------release-------------
                    |                             |
out[5]: ------------alloc-------------------------release-------------
                    |                             |
out[6]: ------------alloc-------------------------release-------------
                    |                             |
out[7]: ------------alloc-------------------------release-------------
        """,
                                    use_unicode_characters=False)
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)))
Beispiel #19
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)]
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')