Skip to content
Merged
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
622 commits
Select commit Hold shift + click to select a range
086dde1
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 2, 2025
b18f8fc
Merge branch 'feature/variables' into feature/subroutines
comp-phys-marc Jun 2, 2025
3f053bb
Merge branch 'feature/end-statements' into feature/variables
comp-phys-marc Jun 2, 2025
776d6c4
Run formatter
comp-phys-marc Jun 2, 2025
afb2969
Pass Context instead of dict
comp-phys-marc Jun 2, 2025
6546d4c
Use dataclass for Variables
comp-phys-marc Jun 2, 2025
b771aa5
Feedback
comp-phys-marc Jun 2, 2025
db29b72
PR Feedback
comp-phys-marc Jun 2, 2025
e7c5c75
Move retrieve_variable into Context
comp-phys-marc Jun 2, 2025
219fe4d
Format
comp-phys-marc Jun 2, 2025
aa0883a
Update docs
comp-phys-marc Jun 2, 2025
2be2c10
Merge branch 'feature/end-statements' into feature/variables
comp-phys-marc Jun 2, 2025
d274a8e
Improve test coverage
comp-phys-marc Jun 3, 2025
6e57e3f
More test coverage
comp-phys-marc Jun 3, 2025
5d5c632
Use set op instead of loop to build wire list
comp-phys-marc Jun 3, 2025
199f46e
PR Feedback
comp-phys-marc Jun 3, 2025
5ece1da
Merge remote-tracking branch 'origin/feature/variables' into feature/…
comp-phys-marc Jun 3, 2025
ac9a5dd
Debug failing regex
comp-phys-marc Jun 3, 2025
0c30fa2
Casting
comp-phys-marc Jun 3, 2025
36e1c11
Improve coverage
comp-phys-marc Jun 3, 2025
b7b98c4
Merge branch 'master' into feature/variables
comp-phys-marc Jun 3, 2025
8c34b18
Move exprs and casts to separate PR
comp-phys-marc Jun 3, 2025
650fdf9
Simple expressions and casts
comp-phys-marc Jun 3, 2025
d8ce679
pylint
comp-phys-marc Jun 3, 2025
c5d1355
pylint
comp-phys-marc Jun 3, 2025
b886aea
Refactor pylint directives
comp-phys-marc Jun 3, 2025
c8c24be
Change TypeError -> NotImplementedError
comp-phys-marc Jun 3, 2025
30a3227
Change TypeError -> NotImplementedError in tests
comp-phys-marc Jun 3, 2025
2ed517a
Merge remote-tracking branch 'origin/feature/variables' into feature/…
comp-phys-marc Jun 3, 2025
6ede81d
PR Feedbback
comp-phys-marc Jun 3, 2025
897bce9
Full file paths for CI
comp-phys-marc Jun 3, 2025
5d2a43e
Format
comp-phys-marc Jun 3, 2025
33c9e04
Trigger CI
comp-phys-marc Jun 4, 2025
a484ea8
Simplify indexing
comp-phys-marc Jun 4, 2025
a6c1cc8
File paths for CI
comp-phys-marc Jun 4, 2025
e804d6b
Handle more general indices
comp-phys-marc Jun 4, 2025
d3b6f76
Merge branch 'feature/variables' into feature/expressions
comp-phys-marc Jun 4, 2025
988c5d1
Implement binary and unary exprs without eval
comp-phys-marc Jun 4, 2025
0b66c1d
pylint
comp-phys-marc Jun 4, 2025
927766f
Improve coverage
comp-phys-marc Jun 4, 2025
e1ee664
Assignment expressions
comp-phys-marc Jun 4, 2025
08d61e0
pylint
comp-phys-marc Jun 4, 2025
33e0aea
pylint
comp-phys-marc Jun 4, 2025
70fa8aa
Test Expressions
comp-phys-marc Jun 4, 2025
30d3180
format
comp-phys-marc Jun 4, 2025
3afd579
Remove redundant logic in retrieve_variable
comp-phys-marc Jun 4, 2025
22d1ba5
Test unary exprs
comp-phys-marc Jun 4, 2025
4d74068
Merge branch 'feature/variables' into feature/expressions
comp-phys-marc Jun 4, 2025
d0e58c4
Merge branch 'feature/expressions' into feature/subroutines
comp-phys-marc Jun 4, 2025
7cfa9d2
Refactor dict to Variable
comp-phys-marc Jun 4, 2025
2ceb1d9
File paths for CI
comp-phys-marc Jun 4, 2025
097a2b1
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 4, 2025
b5961c7
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 4, 2025
4d3f4c4
Refactor after merge
comp-phys-marc Jun 5, 2025
467820b
Roughly equal
comp-phys-marc Jun 5, 2025
59a1248
Switch case
comp-phys-marc Jun 5, 2025
a7353d7
Merge branch 'master' into feature/expressions
comp-phys-marc Jun 6, 2025
0e16fdc
Merge branch 'master', fix switch
comp-phys-marc Jun 6, 2025
4a02d0b
Clean up merge
comp-phys-marc Jun 6, 2025
98a3042
Cleanup tests
comp-phys-marc Jun 6, 2025
fc33e3b
pylint
comp-phys-marc Jun 6, 2025
f5bfd74
Typo in comment
comp-phys-marc Jun 6, 2025
98458c4
PR feedback
comp-phys-marc Jun 6, 2025
ce3b163
Merge remote-tracking branch 'origin' into feature/expressions
comp-phys-marc Jun 6, 2025
10bf4d2
Merge remote-tracking branch 'origin/feature/expressions' into featur…
comp-phys-marc Jun 6, 2025
0fe1c7d
Format
comp-phys-marc Jun 6, 2025
6400876
Merge branch 'feature/expressions' into feature/subroutines
comp-phys-marc Jun 6, 2025
2849bd4
pylint
comp-phys-marc Jun 6, 2025
9006e26
Merge branch 'feature/expressions' into feature/subroutines
comp-phys-marc Jun 6, 2025
6d12397
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 6, 2025
4cf6b49
Cleanup merge
comp-phys-marc Jun 6, 2025
28ccbf5
File paths for CI
comp-phys-marc Jun 6, 2025
0692899
Improve coverage
comp-phys-marc Jun 9, 2025
51a9975
Format, pylint
comp-phys-marc Jun 9, 2025
5271539
Merge branch 'feature/expressions' into feature/subroutines
comp-phys-marc Jun 9, 2025
ce3ad7f
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 9, 2025
605fc1f
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 9, 2025
1f7fd14
isnumeric doesn't match "."
comp-phys-marc Jun 9, 2025
3733f58
Test organization
comp-phys-marc Jun 9, 2025
5ba2f3a
Merge branch 'feature/expressions' into feature/subroutines
comp-phys-marc Jun 9, 2025
619a257
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 9, 2025
96269d7
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 9, 2025
391c74d
Format
comp-phys-marc Jun 9, 2025
47b90e6
Processing functions
comp-phys-marc Jun 9, 2025
56d8f5f
typo in comment
comp-phys-marc Jun 9, 2025
481b1f4
Minor cosmetic refactor
comp-phys-marc Jun 9, 2025
d208757
Merge remote-tracking branch 'origin/feature/expressions' into featur…
comp-phys-marc Jun 9, 2025
237c776
Update changelog
comp-phys-marc Jun 9, 2025
63e2bdc
Merge branch 'master' into feature/expressions
comp-phys-marc Jun 9, 2025
1b58489
PR feedback
comp-phys-marc Jun 9, 2025
0d6e9a4
Merge branch 'feature/expressions' into feature/subroutines
comp-phys-marc Jun 9, 2025
16676d5
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 9, 2025
f3e40b0
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 9, 2025
867b659
Remove redundant check
comp-phys-marc Jun 9, 2025
72aa5fa
Remove unused key
comp-phys-marc Jun 9, 2025
4f6a80a
Test undefined sub
comp-phys-marc Jun 9, 2025
22d840b
File paths for CI
comp-phys-marc Jun 9, 2025
8da2afe
Merge branch 'master' into feature/subroutines
comp-phys-marc Jun 9, 2025
91646ec
[WIP] processing measurementvalues in exprs
comp-phys-marc Jun 9, 2025
6627fba
format, pylint
comp-phys-marc Jun 9, 2025
6d62940
Test classical arg
comp-phys-marc Jun 9, 2025
d2674b2
Only make constants accessible on inner scope's vars
comp-phys-marc Jun 9, 2025
711a479
Remove outer scopes
comp-phys-marc Jun 9, 2025
13c6f84
Remove outer wires
comp-phys-marc Jun 9, 2025
d00aa12
Remove unreachable branch
comp-phys-marc Jun 9, 2025
a749de8
Update changelog
comp-phys-marc Jun 9, 2025
663b7ac
processing measurementvalues in exprs
comp-phys-marc Jun 10, 2025
8a2771f
Unary ops and measurements
comp-phys-marc Jun 10, 2025
1eefbe4
Simplify subroutine naming
comp-phys-marc Jun 10, 2025
bf0db2a
[WIP] test post processing
comp-phys-marc Jun 10, 2025
fc8fe3b
Cleanup minor typo in suggested change
comp-phys-marc Jun 10, 2025
c3417bc
PR Feedback
comp-phys-marc Jun 10, 2025
b2daffe
Pass attribute retrievals through to context dict
comp-phys-marc Jun 10, 2025
73a8d2a
Fix infinite recursion
comp-phys-marc Jun 10, 2025
c648a93
Test post processing
comp-phys-marc Jun 10, 2025
684bc46
Cleanup test
comp-phys-marc Jun 10, 2025
1c17d2f
Test resets
comp-phys-marc Jun 10, 2025
8c204ff
Format
comp-phys-marc Jun 10, 2025
d5c4567
Remove recursion protection - now unecessary
comp-phys-marc Jun 10, 2025
a6f667b
Pass None to getattr instead of catching KeyError
comp-phys-marc Jun 10, 2025
f7e8b94
Revert suggested change
comp-phys-marc Jun 10, 2025
d0278a6
Format, paths for CI
comp-phys-marc Jun 11, 2025
3d20119
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 11, 2025
c22a6c6
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 11, 2025
3e27f82
format
comp-phys-marc Jun 11, 2025
2774bcf
Remove outer_wires, outer_scopes
comp-phys-marc Jun 11, 2025
3851b2b
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 11, 2025
6dc1c9e
Test measurement outside of subroutine
comp-phys-marc Jun 11, 2025
b2983d5
Inputs
comp-phys-marc Jun 11, 2025
d80a23b
Create PR
comp-phys-marc Jun 11, 2025
323be18
Update docstring
comp-phys-marc Jun 11, 2025
9131bfa
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jun 11, 2025
1b2ac89
Update docstring
comp-phys-marc Jun 11, 2025
408f3af
Test naming
comp-phys-marc Jun 11, 2025
a4511ca
Minor fixes
comp-phys-marc Jun 11, 2025
f0a53f8
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 11, 2025
4aa6701
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jun 11, 2025
858d82a
Minor cleanup
comp-phys-marc Jun 11, 2025
6c53100
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 11, 2025
802ecf9
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jun 11, 2025
5b6ed05
Repeated calls test
comp-phys-marc Jun 11, 2025
fe671b1
Test passing
comp-phys-marc Jun 11, 2025
d9fdab3
format
comp-phys-marc Jun 11, 2025
bab7814
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 11, 2025
d91c081
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 11, 2025
2b83310
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jun 11, 2025
82f4eee
replace else if with wlif
comp-phys-marc Jun 11, 2025
268eabb
Trigger CI
comp-phys-marc Jun 11, 2025
a4e2f12
Merge remote-tracking branch 'origin/feature/subroutines' into featur…
comp-phys-marc Jun 11, 2025
cf419af
Init wire map on call and test
comp-phys-marc Jun 11, 2025
b61e01d
path for CI
comp-phys-marc Jun 11, 2025
727a1c1
Reset return
comp-phys-marc Jun 11, 2025
741037e
Stylistic change
comp-phys-marc Jun 11, 2025
68c899d
Resolve wire renamings in outer contexts before new calls
comp-phys-marc Jun 11, 2025
400fd35
Merge remote-tracking branch 'origin/feature/subroutines' into featur…
comp-phys-marc Jun 11, 2025
be4c009
Could get a collision with the first call
comp-phys-marc Jun 11, 2025
51caaa7
Merge branch 'master' into feature/subroutines
comp-phys-marc Jun 11, 2025
712ee90
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 11, 2025
27f9570
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 11, 2025
52a5624
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jun 11, 2025
6f292b0
Minor feedback
comp-phys-marc Jun 13, 2025
36d20fd
Format
comp-phys-marc Jun 13, 2025
2eae3b6
Avoid pointless lookup
comp-phys-marc Jun 13, 2025
37d4aa5
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 13, 2025
3d15137
Format
comp-phys-marc Jun 13, 2025
1b0d3d9
Improve test coverage
comp-phys-marc Jun 13, 2025
f7d8f2e
Handle set declarations and try to simplify loop iterators
comp-phys-marc Jun 13, 2025
999e24a
Format, cleanup
comp-phys-marc Jun 13, 2025
f133db4
Cleanup
comp-phys-marc Jun 13, 2025
4b48978
pylint
comp-phys-marc Jun 13, 2025
979dbce
Control on true rather than false case
comp-phys-marc Jun 13, 2025
839d451
Fix indentation from suggested change
comp-phys-marc Jun 13, 2025
b91e312
Control on true rather than false case
comp-phys-marc Jun 16, 2025
a63117c
Handle scoping of constants
comp-phys-marc Jun 16, 2025
5367de9
Format
comp-phys-marc Jun 16, 2025
431ed0e
Merge branch 'feature/subroutines' into feature/control-flow
comp-phys-marc Jun 16, 2025
100d7f7
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 16, 2025
fe42a88
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jun 16, 2025
aa04138
pylint
comp-phys-marc Jun 16, 2025
b2cae8c
Debug unrelated test failing in CI
comp-phys-marc Jun 16, 2025
dfeb535
Unecessary partial use
comp-phys-marc Jun 16, 2025
a970daa
Remove erroneous lines in suggested change
comp-phys-marc Jun 16, 2025
237f039
Format
comp-phys-marc Jun 16, 2025
7e871e5
Debug failing docs build in CI
comp-phys-marc Jun 16, 2025
aa0af14
More formal wording in docstring.
comp-phys-marc Jun 16, 2025
97a9ec7
More formal wording in docstring.
comp-phys-marc Jun 16, 2025
5e1b0e1
More formal wording in docstring.
comp-phys-marc Jun 16, 2025
5adb8ea
Refactor import for brevity
comp-phys-marc Jun 17, 2025
6440669
Merge remote-tracking branch 'origin/feature/control-flow' into featu…
comp-phys-marc Jun 17, 2025
32d5977
Format
comp-phys-marc Jun 17, 2025
b337dba
Remove ast. prefix from docstrings
comp-phys-marc Jun 17, 2025
7eecdb4
PR feedback
comp-phys-marc Jun 17, 2025
157615f
Raise using MCM as while condition
comp-phys-marc Jun 17, 2025
5961d8d
pylint
comp-phys-marc Jun 17, 2025
4527a58
Trigger CI
comp-phys-marc Jun 17, 2025
71a6090
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 17, 2025
651d385
Paths for CI
comp-phys-marc Jun 17, 2025
c12faba
format
comp-phys-marc Jun 17, 2025
80cbade
Clean up tests
comp-phys-marc Jun 17, 2025
5d59b6d
Mudit's feedback
comp-phys-marc Jun 17, 2025
e3be2b2
Test of nested control flow
comp-phys-marc Jun 17, 2025
bf16181
Add empty newline at end of file
comp-phys-marc Jun 17, 2025
e796806
Add empty newline at end of file
comp-phys-marc Jun 17, 2025
31d6a92
Add empty newline at end of file
comp-phys-marc Jun 17, 2025
d28cb73
Update docstring
comp-phys-marc Jun 18, 2025
adda770
Add newlines to ends of .qasm files
comp-phys-marc Jun 18, 2025
3b0bab8
Typo in comment
comp-phys-marc Jun 18, 2025
c3ebd93
Reword docstring
comp-phys-marc Jun 18, 2025
57e8c81
Tighten type hint on unary operands
comp-phys-marc Jun 18, 2025
1c41686
Merge remote-tracking branch 'origin/feature/measurement' into featur…
comp-phys-marc Jun 18, 2025
796bb31
Revert type hint change
comp-phys-marc Jun 18, 2025
2d9a177
Update test
comp-phys-marc Jun 18, 2025
5e7d80a
Merge branch 'master' into feature/control-flow
comp-phys-marc Jun 18, 2025
20e4e4b
Merge remote-tracking branch 'origin/feature/control-flow' into featu…
comp-phys-marc Jun 18, 2025
263f129
Improve type hints
comp-phys-marc Jun 18, 2025
838da90
Codefactor
comp-phys-marc Jun 18, 2025
ef17074
Codefactor
comp-phys-marc Jun 18, 2025
59044a1
format
comp-phys-marc Jun 18, 2025
57673a8
Simplify loops scopes
comp-phys-marc Jun 18, 2025
25eb22e
Oops I over simplified
comp-phys-marc Jun 18, 2025
0edf61f
Simplify by removing unecessary block scopes
comp-phys-marc Jun 18, 2025
d322e21
Paths for CI
comp-phys-marc Jun 18, 2025
a9cc3db
format
comp-phys-marc Jun 18, 2025
8ab4f95
__getitem__ no longer necessary or helpful
comp-phys-marc Jun 18, 2025
c6ed0a2
Simplify 3 lines -> 1 line
comp-phys-marc Jun 21, 2025
bd341b3
Rename _handle_break -> execute_loop
comp-phys-marc Jun 21, 2025
93ec021
Trigger CI
comp-phys-marc Jun 21, 2025
b140f19
Trigger CI
comp-phys-marc Jun 23, 2025
ca527ea
Merge remote-tracking branch 'origin/feature/control-flow' into featu…
comp-phys-marc Jun 23, 2025
b4641d1
Merge branch 'master' into feature/control-flow
comp-phys-marc Jun 23, 2025
f1acac2
Merge branch 'master' into feature/control-flow
comp-phys-marc Jun 23, 2025
8c56393
Merge branch 'feature/control-flow' into feature/measurement
comp-phys-marc Jun 23, 2025
0e27170
Allow subscripting of Context - helpful now
comp-phys-marc Jun 23, 2025
1b11f54
Merge branch 'master' into feature/measurement
comp-phys-marc Jun 23, 2025
e2208d3
PR feedback
comp-phys-marc Jun 30, 2025
90cb251
Merge branch 'master' into feature/measurement
comp-phys-marc Jun 30, 2025
403d316
Add PR to changelog
comp-phys-marc Jun 30, 2025
993d203
Merge branch 'feature/measurement' into feature/inputs
comp-phys-marc Jul 2, 2025
9706dff
Merge branch 'master' into feature/inputs
comp-phys-marc Jul 3, 2025
c0b0f05
Raise if too many / incorrect params are passed
comp-phys-marc Jul 3, 2025
70ede3d
Docs
comp-phys-marc Jul 3, 2025
b4b275a
scope name
comp-phys-marc Jul 3, 2025
98f6735
pylint
comp-phys-marc Jul 4, 2025
eaa85a2
Merge branch 'master' into feature/inputs
comp-phys-marc Jul 10, 2025
582ea90
Cleanup merge
comp-phys-marc Jul 10, 2025
0e94c47
Merge branch 'master' into feature/inputs
comp-phys-marc Jul 14, 2025
c2a2ad0
Pass kwargs through returned func
comp-phys-marc Jul 14, 2025
1fa3ffa
Remove redundant kwargs
comp-phys-marc Jul 14, 2025
a50ddc9
cleanup test
comp-phys-marc Jul 14, 2025
fc13f7f
pylint
comp-phys-marc Jul 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

<h3>New features since last release</h3>

<h4>OpenQASM 🤝 PennyLane</h4>

* More functionality within :func:`qml.from_qasm3` has been added, which converts more complex OpenQASM 3.0 circuits
into quantum functions that can be subsequently loaded into QNodes and executed. For more details,
please consult the documentation for :func:`qml.from_qasm3`.
[(#7651)](https://github.com/PennyLaneAI/pennylane/pull/7651)

<h3>Improvements 🛠</h3>

<h3>Breaking changes 💔</h3>
Expand Down
6 changes: 3 additions & 3 deletions pennylane/io/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,14 +843,14 @@ def from_quil_file(quil_filename: str):
return plugin_converter(quil_filename)


def from_qasm3(quantum_circuit: str, wire_map: dict = None):
def from_qasm3(quantum_circuit: str, wire_map: dict = None, **kwargs):
"""
Converts an OpenQASM 3.0 circuit into a quantum function that can be used within a QNode.

.. note::
The following OpenQASM 3.0 gates are not supported: sdg, tdg, cu. Built-in mathematical functions and
constants, custom gates, and pulses are not yet supported. The remaining standard library gates,
subroutines, variables, control flow, measurements and ``end`` statements are all supported.
subroutines, variables, control flow, measurements, inputs and ``end`` statements are all supported.

In order to use this function, ``openqasm3`` and ``'openqasm3[parser]'`` must be installed in the user's
environment. Please consult the `OpenQASM installation instructions <https://pypi.org/project/openqasm3>`__
Expand Down Expand Up @@ -939,6 +939,6 @@ def my_circuit():
) from e

def interpret_function():
QasmInterpreter().interpret(ast, context={"name": "global", "wire_map": wire_map})
QasmInterpreter().interpret(ast, context={"name": "global", "wire_map": wire_map}, **kwargs)

return interpret_function
42 changes: 40 additions & 2 deletions pennylane/io/qasm_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ class QasmInterpreter:
visitor function on each node.
"""

def __init__(self):
"""
Initializes the QASM interpreter.
"""
self.inputs = {}
self.found_inputs = []

@functools.singledispatchmethod
def visit(self, node: QASMNode, context: Context, aliasing: bool = False):
"""
Expand Down Expand Up @@ -427,18 +434,23 @@ def visit_list(self, node_list: list, context: Context):
for sub_node in node_list:
self.visit(sub_node, context)

def interpret(self, node: QASMNode, context: dict):
def interpret(self, node: QASMNode, context: dict, **inputs):
"""
Entry point for visiting the QASMNodes of a parsed OpenQASM 3.0 program.

Args:
node (QASMNode): The top-most QASMNode.
context (Context): The initial context populated with the name of the program (the outermost scope).
context (dict): The initial context populated with the name of the program (the outermost scope).
inputs (dict): Additional inputs to the OpenQASM 3.0 program.

Raises:
ValueError: If the wrong parameters are provided in **inputs.

Returns:
dict: The context updated after the compilation of all nodes by the visitor.
"""
context = Context(context)
self.inputs = inputs

# begin recursive descent traversal
try:
Expand All @@ -450,6 +462,12 @@ def interpret(self, node: QASMNode, context: dict):
self.visit(item, context)
except EndProgram:
pass

if len(self.found_inputs) != len(inputs):
raise ValueError(
f"Got the wrong input parameters {list(inputs.keys())} to QASM, expecting {self.found_inputs}."
)

return context

@visit.register(ast.QuantumMeasurement)
Expand Down Expand Up @@ -805,6 +823,26 @@ def _index_into_var(
f"Array index does not evaluate to a single RangeDefinition or Literal at line {node.span.start_line}."
)

@visit.register(ast.IODeclaration)
def visit_io_declaration(self, node: ast.IODeclaration, context: Context):
"""
Registers an input declaration (outputs to come in a future PR).

Args:
node (IODeclaration): The IODeclaration QASMNode.
context (Context): the current context.
"""
name = _resolve_name(node.identifier)
self.found_inputs.append(name)
if name in self.inputs:
context.vars[name] = Variable(
node.type.__class__.__name__, self.inputs[name], -1, node.span.start_line, True
)
else:
raise ValueError(
f"Missing input {name}. Please pass {name} as a keyword argument to from_qasm3."
)

@visit.register(ast.EndStatement)
def visit_end_statement(self, node: ast.EndStatement, context: Context):
"""
Expand Down
60 changes: 60 additions & 0 deletions tests/io/qasm_interpreter/test_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,66 @@
pass


@pytest.mark.external
class TestIO:

def test_wrong_input(self):
ast = parse(
"""
input float theta;
qubit q;
rx(theta) q;
"""
)

with pytest.raises(
ValueError,
match=escape(
"Got the wrong input parameters ['theta', 'phi'] to QASM, expecting ['theta']."
),
):
QasmInterpreter().interpret(
ast, context={"name": "wrong-input", "wire_map": None}, theta=0.2, phi=0.1
)

def test_missing_input(self):
ast = parse(
"""
input float theta;
qubit q;
rx(theta) q;
"""
)

with pytest.raises(
ValueError,
match="Missing input theta. Please pass theta as a keyword argument to from_qasm3.",
):
QasmInterpreter().interpret(ast, context={"name": "missing-input", "wire_map": None})

def test_input(self):
ast = parse(
"""
input float theta;
qubit q;
rx(theta) q;
"""
)

with queuing.AnnotatedQueue() as q:
QasmInterpreter().interpret(
ast, context={"name": "inputs", "wire_map": None}, theta=0.1
)
QasmInterpreter().interpret(
ast, context={"name": "inputs", "wire_map": None}, theta=0.2
)

assert q.queue == [
RX(0.1, "q"),
RX(0.2, "q"),
]


@pytest.mark.external
class TestMeasurementReset:

Expand Down