Skip to content

Commit

Permalink
Test (and fix!) QReference toplevel bindings.
Browse files Browse the repository at this point in the history
  • Loading branch information
blakejohnson committed Apr 11, 2017
1 parent 032f024 commit 3f53a0c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/python/pyqgl2/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,13 @@ def visit(self, orig_node):
qbit_preamble.append(stmnt.body[0])
elif isinstance(v, QReference):
# make a new QRegister containing the referenced qubits
idx = range(v.ref)[v.idx]
qubit_args = ", ".join("q" + str(n) for n in v.ref.qubits[idx])
stmnt = ast.parse("{0} = QRegister({1})".format(v, qubit_args))
if isinstance(v.idx, slice):
qubit_args = ", ".join("'q{0}'".format(n) for n in v.ref.qubits[v.idx])
else:
qubit_args = "'q{0}'".format(v.ref.qubits[v.idx])
stmnt = ast.parse("{0} = QRegister({1})".format(k, qubit_args))
copy_all_loc(stmnt.body[0], node, recurse=True)
qbit_preamble.append(stmnt.body[0])
elif hasattr(v, '__iter__') and all(isinstance(x, QRegister) for x in v):
# a uniform list of QRegisters
tmp_namer = TempVarManager.create_temp_var_manager(
Expand Down
37 changes: 37 additions & 0 deletions test/test_toplevel_binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,43 @@ def test_main3(self):

self.assertEqual(seqs, expectedseq)

def test_main3b(self):
# QReference input
q1 = QubitFactory('q1')
qr = QRegister('q1', 'q2')
amps = range(5)
expectedseq = [Xtheta(q1, amp=a) for a in amps]

# tuple input for toplevel_bindings
resFunction = compile_function(
"test/code/toplevel_binding.py",
"main3",
(qr[0], amps)
)
seqs = resFunction()

self.assertEqual(seqs, expectedseq)

def test_main3c(self):
# QReference slice
q1 = QubitFactory('q1')
q2 = QubitFactory('q2')
qr = QRegister('q1', 'q2', 'q3')
amps = range(5)
expectedseq = []
for a in amps:
expectedseq += [Xtheta(q1, amp=a), Xtheta(q2, amp=a)]

# tuple input for toplevel_bindings
resFunction = compile_function(
"test/code/toplevel_binding.py",
"main3",
(qr[:2], amps)
)
seqs = resFunction()

self.assertEqual(seqs, expectedseq)

def test_main4(self):
# add a function handle as an input
q1 = QubitFactory('q1')
Expand Down

0 comments on commit 3f53a0c

Please sign in to comment.