Skip to content
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

Re-allow non-standard includes in OpenQASM 3 exporter (backport #13148) #13185

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 13 additions & 6 deletions qiskit/qasm3/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,16 @@ def __init__(
):
"""
Args:
includes: the filenames that should be emitted as includes. These files will be parsed
for gates, and any objects dumped from this exporter will use those definitions
where possible.
includes: the filenames that should be emitted as includes.

.. note::

At present, only the standard-library file ``stdgates.inc`` is properly
understood by the exporter, in the sense that it knows the gates it defines.
You can specify other includes, but you will need to pass the names of the gates
they define in the ``basis_gates`` argument to avoid the exporter outputting a
separate ``gate`` definition.

basis_gates: the basic defined gate set of the backend.
disable_constants: if ``True``, always emit floating-point constants for numeric
parameter values. If ``False`` (the default), then values close to multiples of
Expand Down Expand Up @@ -675,9 +682,9 @@ def build_program(self):
def build_includes(self):
"""Builds a list of included files."""
for filename in self.includes:
if (definitions := _KNOWN_INCLUDES.get(filename)) is None:
raise QASM3ExporterError(f"Unknown OpenQASM 3 include file: '{filename}'")
for name, gate in definitions.items():
# Note: unknown include files have a corresponding `include` statement generated, but do
# not actually define any gates; we rely on the user to pass those in `basis_gates`.
for name, gate in _KNOWN_INCLUDES.get(filename, {}).items():
self.symbols.register_gate_without_definition(name, gate)
yield ast.Include(filename)

Expand Down
11 changes: 11 additions & 0 deletions releasenotes/notes/qasm3-includes-ceb56f49b8c190ff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
The OpenQASM 3 exporter has restored its behavior of accepting non-standard-library include
files in the ``includes`` argument to :func:`.qasm3.dump`, :func:`~.qasm3.dumps`, and
:class:`~.qasm3.Exporter`. These will insert a suitable ``include`` statement into the output
as before, and the exporter remains unaware of the intended gates in that include file; you
should pass the gates you expect it to define in the ``basis_gates`` argument to the same functions.
We expect to improve the export mechanism against non-standard include files in a future release
of Qiskit.
19 changes: 19 additions & 0 deletions test/python/qasm3/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,25 @@ def test_no_include(self):
"""
self.assertEqual(Exporter(includes=[]).dumps(circuit), expected_qasm)

def test_include_unknown_file(self):
"""Test export can target a non-standard include without complaints."""
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])

expected = """\
OPENQASM 3.0;
include "mygates.inc";
bit[2] c;
qubit[2] q;
h q[0];
cx q[0], q[1];
c[0] = measure q[0];
c[1] = measure q[1];
"""
self.assertEqual(dumps(qc, includes=["mygates.inc"], basis_gates=["h", "cx"]), expected)

def test_teleportation(self):
"""Teleportation with physical qubits"""
qc = QuantumCircuit(3, 2)
Expand Down
Loading