def test_binary_classicals(): p = Program() # OR is deprecated in favor of IOR with pytest.warns(UserWarning): p.inst(OR(MemoryReference("ro", 1), MemoryReference("ro", 0))) p.inst( AND(MemoryReference("ro", 0), MemoryReference("ro", 1)), MOVE(MemoryReference("ro", 0), MemoryReference("ro", 1)), CONVERT(MemoryReference("ro", 0), MemoryReference("ro", 1)), IOR(MemoryReference("ro", 0), MemoryReference("ro", 1)), XOR(MemoryReference("ro", 0), MemoryReference("ro", 1)), ADD(MemoryReference("ro", 0), MemoryReference("ro", 1)), SUB(MemoryReference("ro", 0), MemoryReference("ro", 1)), MUL(MemoryReference("ro", 0), MemoryReference("ro", 1)), DIV(MemoryReference("ro", 0), MemoryReference("ro", 1)), EXCHANGE(MemoryReference("ro", 0), MemoryReference("ro", 1)), ) assert (p.out() == "IOR ro[0] ro[1]\n" "AND ro[0] ro[1]\n" "MOVE ro[0] ro[1]\n" "CONVERT ro[0] ro[1]\n" "IOR ro[0] ro[1]\n" "XOR ro[0] ro[1]\n" "ADD ro[0] ro[1]\n" "SUB ro[0] ro[1]\n" "MUL ro[0] ro[1]\n" "DIV ro[0] ro[1]\n" "EXCHANGE ro[0] ro[1]\n")
def test_memory_reference_unpacking(): p = Program() p.inst( AND("ro", ("ro", 1)), MOVE("ro", ("ro", 1)), CONVERT("ro", ("ro", 1)), IOR("ro", ("ro", 1)), XOR("ro", ("ro", 1)), ADD("ro", ("ro", 1)), SUB("ro", ("ro", 1)), MUL("ro", ("ro", 1)), DIV("ro", ("ro", 1)), EXCHANGE("ro", ("ro", 1)), ) assert (p.out() == "AND ro[0] ro[1]\n" "MOVE ro[0] ro[1]\n" "CONVERT ro[0] ro[1]\n" "IOR ro[0] ro[1]\n" "XOR ro[0] ro[1]\n" "ADD ro[0] ro[1]\n" "SUB ro[0] ro[1]\n" "MUL ro[0] ro[1]\n" "DIV ro[0] ro[1]\n" "EXCHANGE ro[0] ro[1]\n")
def test_memory_commands(): parse_equals( "DECLARE mem OCTET[32] SHARING mem2 OFFSET 16 REAL OFFSET 32 REAL", Declare("mem", "OCTET", 32, shared_region="mem2", offsets=[(16, "REAL"), (32, "REAL")]), ) parse_equals("STORE mem ro[2] ro[0]", STORE("mem", MemoryReference("ro", 2), MemoryReference("ro", 0))) parse_equals("STORE mem ro[2] 7", STORE("mem", MemoryReference("ro", 2), 7)) parse_equals("LOAD ro[8] mem mem[4]", LOAD(MemoryReference("ro", 8), "mem", MemoryReference("mem", 4))) parse_equals("CONVERT ro[1] ro[2]", CONVERT(MemoryReference("ro", 1), MemoryReference("ro", 2))) parse_equals("EXCHANGE ro[0] ro[1]", EXCHANGE(MemoryReference("ro", 0), MemoryReference("ro", 1))) parse_equals("MOVE mem[2] 4", MOVE(MemoryReference("mem", 2), 4)) parse_equals("MOVE mem[2] -4", MOVE(MemoryReference("mem", 2), -4)) parse_equals("MOVE mem[2] -4.1", MOVE(MemoryReference("mem", 2), -4.1))
def test_all_instructions(): pq = Program(H(0), X(1), RX(1.2, 2), CNOT(0, 1), CCNOT(0, 1, 2)) ro = pq.declare("ro") pq.measure(0, ro) pq.defgate("mygate", [[1, 0], [0, 1]]) pq.inst(("mygate", 0)) pq.reset(0) pq += Program(NEG(ro), AND(ro, ro), ADD(ro, 1), EQ(ro, ro, ro)) pq += Program(EXCHANGE(ro, ro), CONVERT(ro, ro)) pq += Program(LOAD(ro, ro, ro), STORE(ro, ro, ro)) G = QuilControlFlowGraph(pq) assert len(G.blocks) == 1 assert set(G.nodes) == set([0]) assert set(G.edges) == set() assert G.is_dag()
def test_binary_classicals(): p = Program() p.inst(AND(Addr(0), Addr(1)), OR(Addr(1), Addr(0)), MOVE(Addr(0), Addr(1)), CONVERT(Addr(0), Addr(1)), IOR(Addr(0), Addr(1)), XOR(Addr(0), Addr(1)), ADD(Addr(0), Addr(1)), SUB(Addr(0), Addr(1)), MUL(Addr(0), Addr(1)), DIV(Addr(0), Addr(1)), EXCHANGE(Addr(0), Addr(1))) assert p.out() == 'AND ro[0] ro[1]\n' \ 'IOR ro[0] ro[1]\n' \ 'MOVE ro[0] ro[1]\n' \ 'CONVERT ro[0] ro[1]\n' \ 'IOR ro[0] ro[1]\n' \ 'XOR ro[0] ro[1]\n' \ 'ADD ro[0] ro[1]\n' \ 'SUB ro[0] ro[1]\n'\ 'MUL ro[0] ro[1]\n' \ 'DIV ro[0] ro[1]\n' \ 'EXCHANGE ro[0] ro[1]\n'
def test_memory_reference_unpacking(): p = Program() p.inst(AND("ro", ("ro", 1)), MOVE("ro", ("ro", 1)), CONVERT("ro", ("ro", 1)), IOR("ro", ("ro", 1)), XOR("ro", ("ro", 1)), ADD("ro", ("ro", 1)), SUB("ro", ("ro", 1)), MUL("ro", ("ro", 1)), DIV("ro", ("ro", 1)), EXCHANGE("ro", ("ro", 1))) assert p.out() == 'AND ro[0] ro[1]\n' \ 'MOVE ro[0] ro[1]\n' \ 'CONVERT ro[0] ro[1]\n' \ 'IOR ro[0] ro[1]\n' \ 'XOR ro[0] ro[1]\n' \ 'ADD ro[0] ro[1]\n' \ 'SUB ro[0] ro[1]\n'\ 'MUL ro[0] ro[1]\n' \ 'DIV ro[0] ro[1]\n' \ 'EXCHANGE ro[0] ro[1]\n'
def test_binary_classicals(): p = Program() p.inst(AND(MemoryReference("ro", 0), MemoryReference("ro", 1)), OR(MemoryReference("ro", 1), MemoryReference("ro", 0)), MOVE(MemoryReference("ro", 0), MemoryReference("ro", 1)), CONVERT(MemoryReference("ro", 0), MemoryReference("ro", 1)), IOR(MemoryReference("ro", 0), MemoryReference("ro", 1)), XOR(MemoryReference("ro", 0), MemoryReference("ro", 1)), ADD(MemoryReference("ro", 0), MemoryReference("ro", 1)), SUB(MemoryReference("ro", 0), MemoryReference("ro", 1)), MUL(MemoryReference("ro", 0), MemoryReference("ro", 1)), DIV(MemoryReference("ro", 0), MemoryReference("ro", 1)), EXCHANGE(MemoryReference("ro", 0), MemoryReference("ro", 1))) assert p.out() == 'AND ro[0] ro[1]\n' \ 'IOR ro[0] ro[1]\n' \ 'MOVE ro[0] ro[1]\n' \ 'CONVERT ro[0] ro[1]\n' \ 'IOR ro[0] ro[1]\n' \ 'XOR ro[0] ro[1]\n' \ 'ADD ro[0] ro[1]\n' \ 'SUB ro[0] ro[1]\n'\ 'MUL ro[0] ro[1]\n' \ 'DIV ro[0] ro[1]\n' \ 'EXCHANGE ro[0] ro[1]\n'