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])
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)
def simulate_program(cls, pr: Program, qpu: CommonQPU, circ_args: Dict[str, Any] = {}, job_args: Dict[str, Any] = {}): cr = pr.to_circ(**circ_args) jb = cr.to_job(**job_args) res = qpu.submit(jb) return res
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)
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))
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))
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))