From 526a5dcf5e316ea91c28f84cbf2d72bd4b9943db Mon Sep 17 00:00:00 2001 From: Matthew Harrigan Date: Wed, 7 Nov 2018 10:12:50 -0800 Subject: [PATCH] Sundry --- pyquil/api/_quantum_computer.py | 2 +- pyquil/quil.py | 4 +-- pyquil/quilbase.py | 2 ++ pyquil/tests/test_quantum_computer.py | 6 ++-- pyquil/tests/test_quil.py | 40 +++++++++++++++------------ 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/pyquil/api/_quantum_computer.py b/pyquil/api/_quantum_computer.py index e1c1b4bca..e0e691f05 100644 --- a/pyquil/api/_quantum_computer.py +++ b/pyquil/api/_quantum_computer.py @@ -212,7 +212,7 @@ def run_and_measure(self, program: Program, trials: int) -> Dict[int, np.ndarray into a 2d numpy array of bitstrings, consider:: bitstrings = qc.run_and_measure(...) - bitstring_array = np.vstack(bitstrings[q] for q in sorted(qc.qubits())).T + bitstring_array = np.vstack(bitstrings[q] for q in qc.qubits()).T bitstring_array.shape # (trials, len(qc.qubits())) .. note:: diff --git a/pyquil/quil.py b/pyquil/quil.py index 76a8d5830..bf28031f5 100644 --- a/pyquil/quil.py +++ b/pyquil/quil.py @@ -351,7 +351,7 @@ def while_do(self, classical_reg, q_program): label_start = LabelPlaceholder("START") label_end = LabelPlaceholder("END") self.inst(JumpTarget(label_start)) - self.inst(JumpUnless(target=label_end, condition=Addr(classical_reg))) + self.inst(JumpUnless(target=label_end, condition=classical_reg)) self.inst(q_program) self.inst(Jump(label_start)) self.inst(JumpTarget(label_end)) @@ -632,7 +632,7 @@ def __ne__(self, other): return not self.__eq__(other) def __len__(self): - return len(self._instructions) + return len(self.instructions) def __str__(self): """ diff --git a/pyquil/quilbase.py b/pyquil/quilbase.py index 45fda3f58..f099cfb46 100644 --- a/pyquil/quilbase.py +++ b/pyquil/quilbase.py @@ -304,6 +304,7 @@ class JumpConditional(AbstractInstruction): """ Abstract representation of an conditional jump instruction. """ + op = NotImplemented def __init__(self, target, condition): if not isinstance(target, (Label, LabelPlaceholder)): @@ -400,6 +401,7 @@ class LogicalBinaryOp(AbstractInstruction): """ The abstract class for binary logical classical instructions. """ + op = NotImplemented def __init__(self, left, right): if not isinstance(left, MemoryReference): diff --git a/pyquil/tests/test_quantum_computer.py b/pyquil/tests/test_quantum_computer.py index 8b89ecfd7..fcab6541a 100644 --- a/pyquil/tests/test_quantum_computer.py +++ b/pyquil/tests/test_quantum_computer.py @@ -285,9 +285,11 @@ def test_run_and_measure(local_qvm_quilc): qc = get_qc("9q-generic-qvm") prog = Program(I(8)) trials = 11 - with local_qvm(): # Redundant with test fixture. + # note to devs: this is included as an example in the run_and_measure docstrings + # so if you change it here ... change it there! + with local_qvm(): # Redundant with test fixture. bitstrings = qc.run_and_measure(prog, trials) - bitstring_array = np.vstack(bitstrings[q] for q in sorted(qc.qubits())).T + bitstring_array = np.vstack(bitstrings[q] for q in qc.qubits()).T assert bitstring_array.shape == (trials, len(qc.qubits())) diff --git a/pyquil/tests/test_quil.py b/pyquil/tests/test_quil.py index 53017fef3..145d8691e 100644 --- a/pyquil/tests/test_quil.py +++ b/pyquil/tests/test_quil.py @@ -115,7 +115,7 @@ def test_len_nested(): p = Program(H(0)).measure(0, 0) q = Program(H(0), CNOT(0, 1)) p.if_then(MemoryReference("ro", 0), q) - assert len(p) == 8 + assert len(p) == 9 def test_plus_operator(): @@ -456,22 +456,28 @@ def qft3(q0, q1, q2): def test_control_flows(): - classical_flag_register = 2 - p = Program(X(0), H(0)).measure(0, classical_flag_register) - - # run p in a loop until classical_flag_register is 0 - loop_prog = Program(X(0)).measure(0, classical_flag_register) - loop_prog.while_do(classical_flag_register, p) - assert loop_prog.out() == ('DECLARE ro BIT[3]\n' - 'X 0\n' - 'MEASURE 0 ro[2]\n' - 'LABEL @START1\n' - 'JUMP-UNLESS @END2 ro[2]\n' - 'X 0\n' - 'H 0\n' - 'MEASURE 0 ro[2]\n' - 'JUMP @START1\n' - 'LABEL @END2\n') + outer_loop = Program() + classical_flag_register = outer_loop.declare('classical_flag_register', 'BIT') + outer_loop += MOVE(classical_flag_register, 1) # initialize + + inner_loop = Program() + inner_loop += Program(X(0), H(0)) + inner_loop += MEASURE(0, classical_flag_register) + + # run inner_loop in a loop until classical_flag_register is 0 + outer_loop.while_do(classical_flag_register, inner_loop) + assert outer_loop.out() == '\n'.join([ + "DECLARE classical_flag_register BIT[1]", + "MOVE classical_flag_register 1", + "LABEL @START1", + "JUMP-UNLESS @END2 classical_flag_register", + "X 0", + "H 0", + "MEASURE 0 classical_flag_register", + "JUMP @START1", + "LABEL @END2", + "" + ]) def test_control_flows_2():