-
Notifications
You must be signed in to change notification settings - Fork 11
Simplified implementation of gate modifiers #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0d26440
e842e37
656c037
c2168e8
a70e143
9b91885
4626ee8
2d15ed0
e0065ee
500d42e
f17d8a3
ef7e9bd
c967216
b46f1b1
42ca695
e130748
efc2fdd
19f7e93
5a9c725
0e42b62
fc69d45
091f206
8cf09a3
fddc320
7f1cd86
6123c98
f836a04
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -496,6 +496,8 @@ def test_add_incomptible_type(): | |
def test_measure_reset_pragma(): | ||
prog = Program() | ||
q = PhysicalQubits[0] | ||
with pytest.raises(AssertionError): | ||
reg = PhysicalQubits[0:1] | ||
c = BitVar(name="c") | ||
prog.reset(q) | ||
prog.pragma("CLASSIFIER linear") | ||
|
@@ -2246,6 +2248,58 @@ def g() -> int[32] { | |
).strip() | ||
|
||
assert prog.to_qasm() == expected | ||
_check_respects_type_hints(prog) | ||
|
||
|
||
def test_gate_modifiers(): | ||
prog = oqpy.Program() | ||
qreg = [oqpy.PhysicalQubits[i] for i in range(0, 8)] | ||
six_qubit_reg = [qreg[i] for i in [1, 4, 7, 2, 3, 5]] | ||
|
||
prog.gate(qreg[2], "t", control=qreg[1]) | ||
prog.gate(qreg[2], "x", neg_control=qreg[1]) | ||
prog.gate(qreg[3], "rz", inv=True) | ||
prog.gate(qreg[2], "t", exp=0.5) | ||
prog.gate(qreg[0], "x", inv=True, exp=oqpy.IntVar(5, "i") / 2) | ||
|
||
prog.gate( | ||
six_qubit_reg[-1], | ||
"x", | ||
control=six_qubit_reg[0:2], | ||
neg_control=six_qubit_reg[2:5], | ||
inv=True, | ||
exp=1 / 2, | ||
) | ||
|
||
prog.gate( | ||
qreg[6], | ||
"rz1", | ||
control=[qreg[2], qreg[4], qreg[5]], | ||
neg_control=[qreg[0], qreg[1], qreg[0]], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason to allow repetition here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a byproduct of using sets. I just want to test here that controlling twice on the same qubit outputs an OQ code with a single ctrl modifier. I prefer this behavior, which allows users to pass overlapping qubit lists like |
||
) | ||
|
||
with pytest.raises(ValueError): | ||
prog.gate(qreg[2], "t", control=qreg[2]) | ||
with pytest.raises(ValueError): | ||
prog.gate(qreg[2], "x", neg_control=qreg[2]) | ||
with pytest.raises(ValueError): | ||
prog.gate(qreg[1], "x", control=qreg[2], neg_control=qreg[2]) | ||
|
||
expected = textwrap.dedent( | ||
""" | ||
OPENQASM 3.0; | ||
int[32] i = 5; | ||
ctrl @ t $1, $2; | ||
negctrl @ x $1, $2; | ||
inv @ rz $3; | ||
pow(0.5) @ t $2; | ||
inv @ pow(i / 2) @ x $0; | ||
ctrl(2) @ negctrl(3) @ inv @ pow(0.5) @ x $1, $4, $2, $3, $7, $5; | ||
ctrl(3) @ negctrl(2) @ rz1 $2, $4, $5, $0, $1, $6; | ||
""" | ||
).strip() | ||
assert prog.to_qasm() == expected | ||
_check_respects_type_hints(prog) | ||
|
||
|
||
def test_invalid_gates(): | ||
|
@@ -2266,6 +2320,7 @@ def test_invalid_gates(): | |
def test_gate_declarations(): | ||
prog = oqpy.Program() | ||
q = oqpy.Qubit("q", needs_declaration=False) | ||
r = oqpy.Qubit("r", needs_declaration=False) | ||
with oqpy.gate( | ||
prog, | ||
q, | ||
|
@@ -2280,6 +2335,10 @@ def test_gate_declarations(): | |
prog.gate(q, "u", theta, 0, 0) | ||
with oqpy.gate(prog, q, "t"): | ||
prog.gate(q, "rz", oqpy.pi / 4) | ||
with oqpy.gate(prog, [q, r], "cnot"): | ||
prog.gate(r, "x", control=q) | ||
with oqpy.gate(prog, [q, r], "ncphaseshift", [oqpy.AngleVar(name="theta")]) as theta: | ||
prog.gate(r, "phase", theta, neg_control=[q]) | ||
|
||
prog.gate(oqpy.PhysicalQubits[1], "t") | ||
prog.gate(oqpy.PhysicalQubits[2], "t") | ||
|
@@ -2296,6 +2355,12 @@ def test_gate_declarations(): | |
gate t q { | ||
rz(pi / 4) q; | ||
} | ||
gate cnot q, r { | ||
ctrl @ x q, r; | ||
} | ||
gate ncphaseshift(theta) q, r { | ||
negctrl @ phase(theta) q, r; | ||
} | ||
gate rz(theta) q { | ||
u(theta, 0, 0) q; | ||
} | ||
|
@@ -2305,6 +2370,7 @@ def test_gate_declarations(): | |
).strip() | ||
|
||
assert prog.to_qasm() == expected | ||
_check_respects_type_hints(prog) | ||
|
||
|
||
def test_include(): | ||
|
@@ -2321,6 +2387,7 @@ def test_include(): | |
).strip() | ||
|
||
assert prog.to_qasm() == expected | ||
_check_respects_type_hints(prog) | ||
|
||
|
||
def test_qubit_array(): | ||
|
Uh oh!
There was an error while loading. Please reload this page.