Ejemplo n.º 1
0
    def generate_program(cls, nqbits: int, oracle: Oracle):
        # Add another qubit for output and put it in state \ket{-}
        pr = Program()
        qr = pr.qalloc(nqbits)
        qout = pr.qalloc(1)
        pr.apply(X, qout)
        pr.apply(H, qout)

        for qb in qr:
            pr.apply(H, qb)
        pr.apply(oracle, [*qr, *qout])
        for qb in qr:
            pr.apply(H, qb)
        return pr, qr
Ejemplo n.º 2
0
    def test_default_gates(self):
        prog = Program()
        qreg = prog.qalloc(3)

        for op in pygates_1qb:
            prog.apply(op, qreg[0])

        for op in pygates_2qb:
            prog.apply(op, qreg[0], qreg[1])

        prog.apply(CCNOT, qreg[0], qreg[1], qreg[2])

        expected = prog.to_circ()
        #result = qlm_to_pyquil(qlm_circuit)
        # print(result)
        result = Prg()
        for op in quil_1qb:
            result += op(0)
        for op in quil_params:
            result += op(3.14, 0)

        result += pg.SWAP(0, 1)
        result += pg.CNOT(0, 1)
        for op in quil_ctrl:
            result += op(1).controlled(0)
        for op in quil_ctrl_prm:
            result += op(3.14, 1).controlled(0)

        result += pg.CCNOT(0, 1, 2)

        qlm_circuit = pyquil_to_qlm(result)
        exp_str = print_aq(expected)
        res_str = print_aq(qlm_circuit)
        self.assertEqual(exp_str, res_str)
Ejemplo n.º 3
0
    def test_separate_measures(self):
        prog = Program()
        qreg = prog.qalloc(3)
        creg = prog.calloc(3)

        prog.apply(H, qreg[0])
        prog.apply(H, qreg[1])
        prog.apply(H, qreg[2])

        expected = prog.to_circ()

        result = Prg()
        cbs = result.declare("ro", "BIT", 3)
        result += pg.H(0)
        result += pg.H(1)
        result += pg.H(2)
        result += pg.MEASURE(0, cbs[0])
        result += pg.MEASURE(1, cbs[1])
        result += pg.MEASURE(2, cbs[2])

        result, to_measure = pyquil_to_qlm(result, True)
        exp_str = print_aq(expected)
        res_str = print_aq(result)
        self.assertEqual(res_str, exp_str)
        self.assertEqual(to_measure, [0, 1, 2])
Ejemplo n.º 4
0
 def test_recursive_ctrl_and_dagger(self):
     prog = Program()
     qreg = prog.qalloc(5)
     prog.apply(Y.ctrl().ctrl().ctrl().ctrl().dag().dag().dag(), *qreg)
     expected = prog.to_circ()
     result = Prg()
     result += (pg.Y(4).controlled(0).controlled(1).controlled(
         2).controlled(3).dagger())
     result = pyquil_to_qlm(result)
     res_str = print_aq(result)
     exp_str = print_aq(expected)
     self.assertEqual(res_str, exp_str)
Ejemplo n.º 5
0
    def generate_program(cls, nqbits:int, 
                         oracle: Oracle) -> Tuple[Program, QRegister]:
        # Add another qubit for output and put it in state \ket{-}
        pr = Program()
        qr = pr.qalloc(nqbits)
        iters = ceil(pi / (4 * asin(1 / sqrt(2**nqbits))) - 1 / 2)
        if iters == 0:
            iters = 1
        print(f"Grover iters: {iters}")
        qfun_in = cls._input(nqbits)
        pr.apply(qfun_in, qr)
        for _ in range(iters):
            cls._iter(pr, qr, oracle)

        return pr, qr
Ejemplo n.º 6
0
    def test_recursive_ctrl_and_dagger(self):
        # Create qlm program
        prog = Program()
        qreg = prog.qalloc(5)
        prog.apply(Y.ctrl().ctrl().ctrl().ctrl().dag().dag().dag(), *qreg)
        qlm_circuit = prog.to_circ()
        result = qlm_to_pyquil(qlm_circuit)

        # Create pyquil program
        expected = Prg()
        expected_creg = expected.declare("ro", "BIT", 5)
        expected += (pg.Y(4).controlled(0).controlled(1).controlled(
            2).controlled(3).dagger())
        for qbit, cbit in enumerate(expected_creg):
            expected += pg.MEASURE(qbit, cbit)

        self.assertEqual(str(result), str(expected))
Ejemplo n.º 7
0
    def test_measures(self):
        # Create qlm program
        prog = Program()
        qreg = prog.qalloc(3)

        prog.apply(H, qreg[0])
        prog.apply(H, qreg[1])
        prog.apply(H, qreg[2])

        result = qlm_to_pyquil(prog.to_circ())

        # Create pyquil program
        expected = Prg()
        cbs = expected.declare("ro", "BIT", 3)
        expected += pg.H(0)
        expected += pg.H(1)
        expected += pg.H(2)
        expected += pg.MEASURE(0, cbs[0])
        expected += pg.MEASURE(1, cbs[1])
        expected += pg.MEASURE(2, cbs[2])

        self.assertEqual(str(result), str(expected))
Ejemplo n.º 8
0
    def test_default_gates(self):
        # Create qlm program
        prog = Program()
        qreg = prog.qalloc(3)

        for op in pygates_1qb:
            prog.apply(op, qreg[0])

        for op in pygates_2qb:
            prog.apply(op, qreg[0], qreg[1])

        prog.apply(CCNOT, qreg[0], qreg[1], qreg[2])

        qlm_circuit = prog.to_circ()
        result = qlm_to_pyquil(qlm_circuit)

        # Create pyquil program
        expected = Prg()
        expected_creg = expected.declare("ro", "BIT", 3)
        for op in quil_1qb:
            expected += op(0)
        for op in quil_params:
            expected += op(3.14, 0)

        expected += pg.SWAP(0, 1)
        expected += pg.CNOT(0, 1)
        for op in quil_ctrl:
            expected += op(1).controlled(0)
        for op in quil_ctrl_prm:
            expected += op(3.14, 1).controlled(0)
        expected += pg.CCNOT(0, 1, 2)
        expected += pg.MEASURE(0, expected_creg[0])
        expected += pg.MEASURE(1, expected_creg[1])
        expected += pg.MEASURE(2, expected_creg[2])

        self.assertEqual(str(result), str(expected))