def test_optional():
    def cf(x: qp.IntBuf, y: bool = True):
        x ^= int(y)

    @qp.semi_quantum(classical=cf)
    def qf(x: qp.Qubit, y: qp.Qubit.Borrowed = True):
        x ^= y

    b = qp.IntBuf.raw(val=0, length=1)
    qf.classical(b)
    assert int(b) == 1
    qf.classical(b)
    assert int(b) == 0
    qf.classical(b, True)
    assert int(b) == 1
    qf.classical(b, False)
    assert int(b) == 1

    with qp.Sim() as sim_state:
        q = qp.qalloc()
        assert not sim_state.resolve_location(q, False)
        qf(q)
        assert sim_state.resolve_location(q, False)
        qf(q)
        assert not sim_state.resolve_location(q, False)
        qf(q, True)
        assert sim_state.resolve_location(q, False)
        qf(q, False)
        assert sim_state.resolve_location(q, False)
        assert qp.measure(q, reset=True)
        qp.qfree(q)
Пример #2
0
    def __exit__(self, exc_type, exc_val, exc_tb):
        if len(self.base) >= self.min_len:
            return

        if exc_type is None:
            assert self.padded is not None
            qp.qfree(self.padded)
            self.padded = None
def test_classical():
    def g(x: qp.IntBuf, y: int):
        assert isinstance(x, qp.IntBuf)
        assert isinstance(y, int)
        x ^= y

    @qp.semi_quantum(classical=g)
    def f(x: qp.Quint, y: qp.Quint.Borrowed):
        x ^= y

    assert f.classical is g

    with qp.Sim() as sim_state:
        q = qp.qalloc(len=5)
        assert sim_state.resolve_location(q, False) == 0
        f.sim(sim_state, q, 3)
        assert sim_state.resolve_location(q, False) == 3
        f.sim(sim_state, q, 5)
        assert sim_state.resolve_location(q, False) == 6
        f.sim(sim_state, q, 6)
        qp.qfree(q)
Пример #4
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     qp.qfree(self)
Пример #5
0
 def qfree_as_needed(a: Any):
     if isinstance(a, (qp.Quint, qp.Qureg, qp.Qubit, qp.QuintMod)):
         result = qp.measure(a, reset=True)
         qp.qfree(a)
         return result
     return a