def test_eq(): with qp.Sim(): with qp.qalloc(len=4) as t: t.init(5) for k in range(-2, 60): assert qp.measure(t == k) == (k == 5) assert qp.measure(t, reset=True) == 5
def test_neq(): with qp.Sim(): with qp.qalloc(len=4) as t: t.init(5) for k in range(-2, 60): with qp.hold(t != k) as q: assert qp.measure(q) == (k != 5) assert qp.measure(t, reset=True) == 5
def test_uncompute(): for a, b, p in itertools.product([False, True], repeat=3): with qp.Sim(phase_fixup_bias=p): with qp.hold(a, name='a') as qa: with qp.hold(b, name='b') as qb: with qp.hold(a & b, name='c') as qc: assert qp.measure(qa) == a assert qp.measure(qb) == b assert qp.measure(qc) == (a and b)
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)
def _apply_quantum(func: Callable, kwargs: Dict[str, Any]) -> Dict[str, Any]: with qp.Sim() as sim: type_hints = get_type_hints(func) mapped = {} for k, v in kwargs.items(): if type_hints[k] == qp.Quint: assert isinstance( v, qp.IntBuf ), 'Expected type qp.IntBuf for argument "{}"'.format(k) mapped[k] = qp.qalloc(len=len(v), name=k) mapped[k] ^= int(v) elif type_hints[k] == qp.Qubit: assert isinstance(v, qp.IntBuf) and len( v ) == 1, 'Expected length 1 qp.IntBuf for argument "{}"'.format( k) mapped[k] = qp.qalloc(name=k) mapped[k] ^= int(v) else: mapped[k] = v func(**mapped) result = {} for k, v in kwargs.items(): if type_hints[k] in [qp.Quint, qp.Qubit]: result[k] = qp.measure(mapped[k], reset=True) result['sim_state.phase_degrees'] = sim.phase_degrees return result
def test_sim(): v1 = 15 v2 = 235 offset = 4 bits = 10 with qp.Sim(): with qp.hold(val=v1, name='a') as a: with qp.qalloc(len=bits, name='out') as out: out += a * v2 out += offset result = qp.measure(out, reset=True) assert result == (v1*v2 + offset) & ((1 << bits) - 1)
def test_count(): v1 = 15 v2 = 235 offset = 4 bits = 100 with qp.Sim(): with qp.CountNots() as counts: with qp.hold(val=v1, name='a') as a: with qp.qalloc(len=bits, name='out') as out: out += a * v2 out += offset _ = qp.measure(out, reset=True) assert len(counts.keys()) == 3 assert counts[0] > 0 assert counts[1] > 0 assert 0 < counts[2] <= 1000
def test_cmp(): with qp.Sim(): with qp.qalloc(len=4) as t: t.init(5) for k in range(-5, 30): assert qp.measure(t >= k) == (5 >= k) assert qp.measure(t > k) == (5 > k) assert qp.measure(t < k) == (5 < k) assert qp.measure(k < t) == (k < 5) assert qp.measure(t <= k) == (5 <= k) assert qp.measure(t, reset=True) == 5
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