def test_controls_xor(): def f(c: qp.Qubit, t: qp.Quint): t ^= 1 & qp.controlled_by(c) r = qp.testing.sim_call(f, True, 2) assert r == qp.ArgsAndKwargs([True, 3], {}) r = qp.testing.sim_call(f, False, 2) assert r == qp.ArgsAndKwargs([False, 2], {})
def test_iadd(): def f(a: qp.QuintMod, b: int): a += b r = qp.testing.sim_call( f, a=qp.testing.ModInt(5, 12), b=9, ) assert r == qp.ArgsAndKwargs([], {'a': 2, 'b': 9})
def assert_squares_correctly(n1: int, n2: int, v: int): final_state = qp.testing.sim_call(init_square, factor=qp.IntBuf.raw(val=v, length=n1), clean_out=qp.IntBuf.raw(val=0, length=n2)) assert final_state == qp.ArgsAndKwargs([], { 'factor': v, 'clean_out': v**2 % 2**n2 })
def test_correct_result(method, case): n = case['n'] t = case['t'] k = case['k'] final_state = qp.testing.sim_call(method, target=qp.IntBuf.raw(val=t, length=n), k=k) assert final_state == qp.ArgsAndKwargs([], { 'target': t * k % 2**n, 'k': k, })
def assert_multiplies_correctly(n1: int, n2: int, n3: int, v1: int, v2: int): final_state = qp.testing.sim_call(init_mul, factor1=qp.IntBuf.raw(val=v1, length=n1), factor2=qp.IntBuf.raw(val=v2, length=n2), clean_out=qp.IntBuf.raw(val=0, length=n3)) assert final_state == qp.ArgsAndKwargs([], { 'factor1': v1, 'factor2': v2, 'clean_out': v1 * v2 % 2**n3 })
def test_correct_result(case): n = case['n'] t = case['t'] k = case['k'] def mul(target: qp.Quint, k: int): target *= k final_state = qp.testing.sim_call(mul, target=qp.IntBuf.raw(val=t, length=n), k=k) assert final_state == qp.ArgsAndKwargs([], { 'target': t * k % 2**n, 'k': k, })
def sim_call(func: Callable, *args, **kwargs) -> 'qp.ArgsAndKwargs': def qalloc_as_needed(a: qp.ArgParameter): if a.parameter_type is None: raise ValueError('Function arguments must be annotated, so that ' 'quantum values can be allocated if needed.') if a.parameter_type is qp.Quint: if isinstance(a.arg, qp.IntBuf): n = len(a.arg) elif isinstance(a.arg, int): n = a.arg.bit_length() else: raise ValueError('Unsupported Quint input: {}'.format(a)) result = qp.qalloc(len=n, name=a.parameter.name) result.init(a.arg) return result if a.parameter_type is qp.QuintMod: assert isinstance(a.arg, ModInt) result = qp.qalloc(modulus=a.arg.modulus, name=a.parameter.name) result.init(a.arg.val) return result if a.parameter_type is qp.Qubit: result = qp.qalloc(name=a.parameter.name) result.init(a.arg) return result return a.arg 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 matched = qp.ArgsAndKwargs(args, kwargs).match_parameters(func) with qp.Sim(): allocated = matched.map(qalloc_as_needed) allocated.pass_into(func) return allocated.map(qfree_as_needed)
def test_correct_result(method, case): modulus = case['N'] t = case['t'] k = case['k'] e = case['e'] t %= modulus k %= modulus final_state = qp.testing.sim_call(method, target=qp.testing.ModInt( val=t, modulus=modulus), k=k, e=e) assert final_state == qp.ArgsAndKwargs([], { 'target': (t * k**e) % modulus, 'k': k, 'e': e })
def test_correct_result(method, case): modulus = case['N'] t = case['t'] k = case['k'] y = case['y'] t %= modulus y %= modulus k %= modulus final_state = qp.testing.sim_call(method, target=qp.testing.ModInt( val=t, modulus=modulus), k=k, y=y) assert final_state == qp.ArgsAndKwargs([], { 'target': (t + k * y) % modulus, 'k': k, 'y': y })