Skip to content

Commit

Permalink
Sundry (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpharrigan authored Nov 9, 2018
1 parent f760224 commit 7898853
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pyquil/api/_quantum_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
4 changes: 2 additions & 2 deletions pyquil/quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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):
"""
Expand Down
2 changes: 2 additions & 0 deletions pyquil/quilbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions pyquil/tests/test_quantum_computer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()))


Expand Down
40 changes: 23 additions & 17 deletions pyquil/tests/test_quil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit 7898853

Please sign in to comment.