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 __rixor__(self, other):
        other, controls = qp.ControlledLValue.split(other)
        if controls == qp.QubitIntersection.NEVER:
            return other

        if isinstance(other, qp.Quint):
            t = qp.LookupTable(1 << k for k in range(1 << len(self.binary)))
            other ^= t[self.binary] & qp.controlled_by(controls)
            return other

        return NotImplemented
Exemple #3
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 del_xor_lookup(*,
                   lvalue: 'qp.Quint',
                   table: 'qp.LookupTable',
                   address: 'qp.Quint.Borrowed',
                   control: 'qp.Qubit.Control' = True):
    """Uncomputes a table lookup using measurement based uncomputation."""
    assert isinstance(lvalue, qp.Quint)
    assert isinstance(address, qp.Quint)
    assert isinstance(control,
                      qp.QubitIntersection) and len(control.qubits) <= 1
    table = table[:1 << len(address)]
    address = address[:qp.ceil_lg2(len(table))]

    if all(e == table[0] for e in table):
        qp.IntRValue(table[0]).clear_storage_location(lvalue, control)
        return

    split = min(
        qp.floor_lg2(len(lvalue)),  # Point of no-more-workspace-available.
        len(address) // 2  # Point of optimal operation count.
    )
    low = address[:split]
    high = address[split:]

    with qp.measurement_based_uncomputation(lvalue) as result:
        # Infer whether or not each address has a phase flip.
        raw_fixups = [
            bool(qp.popcnt(result & table[k]) & 1) for k in range(len(table))
        ]
        fixup_table = qp.LookupTable(_chunk_bits(raw_fixups, 1 << split))

        # Fix address phase flips using a smaller table lookup.
        unary_storage = lvalue[:1 << split]
        unary_storage.init(1 << low)
        do_xor_lookup(lvalue=unary_storage,
                      table=fixup_table,
                      address=high,
                      phase_instead_of_toggle=True,
                      control=control)
        unary_storage.clear(1 << low)
Exemple #5
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)
Exemple #6
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)