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

Initial Implementation of a Limited QASM3 Exporter in Rust #13666

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

to24toro
Copy link
Contributor

@to24toro to24toro commented Jan 15, 2025

Fixes #13145

Summary

This PR introduces and integrates several key modules for a Rust-based quantum computing framework, laying the foundation for exporting circuits in QASM 3.0 format. The modules added are:
1. printer.rs:
• Implements functionality for formatted output generation of AST or other hierarchical structures.
2. symbols.rs:
• Manages a symbol table used for mapping variable names to their corresponding entities.
• Provides helper methods for creating, binding, and looking up symbols with robust error handling.
Note: This file is directly copied from Qiskit’s OpenQASM 3 parser, as this symbol table is used when loading QASM 3.0 and converting it to quantum circuits. However, this architecture might be overly complex for the current task of QASM 3.0 exporting, and its necessity should be revisited in future iterations.
3. ast.rs:
• Defines the AST structure for representing quantum programs.
• Contains data structures such as nodes, expressions, and declarations that model the core components of quantum circuit descriptions.
4. exporter.rs:
• Implements a builder for generating QASM 3.0 programs.
• Integrates with Python-based tools (via pyo3) to extract circuit data and parameters.

Details and comments

Current Limitations

The current implementation has limited QASM 3.0 output functionality and relies on partial Python integration. The following features are not yet supported:
• Control Flow Constructs: While QASM 3.0 supports control flow, such as loops and conditionals, these are not yet implemented.
• Classical Variables: Handling classical variables is not included in the current architecture.
• Not standard Gates: Generating QASM for not standard gates and mapping them to Qiskit’s standard gates is pending.

These tasks require further discussion to determine whether they should be included in the scope of Qiskit Issue #13145. My recommendation is to address these features after Qiskit PR #13278 is merged, potentially targeting them for the 2.1 release and beyond.

What is Pending

1. Support for:
• Control flow constructs like loops, conditionals, and subroutines.
• Advanced QASM 3.0 features, including classical variable handling and parameterized gates.
• Handling edge cases for non-standard instructions or unique circuit data.
2. Architecture Alignment:
• Finalizing the architecture based on team feedback to ensure long-term scalability.
• Deciding whether the symbol table from symbols.rs should remain part of the architecture or be replaced with a more streamlined solution.

Discussion Points

1. Scope of Qiskit Issue #13145:
• Should the above pending features (control flow, classical variables, custom gates) be addressed as part of this issue, or deferred to subsequent iterations?
• My suggestion is to revisit these features post-merge of Qiskit PR #13278, targeting them for the 2.1 release or later.
2. Testing and Architecture Validation:
• Before implementing test cases, I would like to confirm agreement on the overall architecture and the scope of tasks to ensure alignment with the team’s priorities.

import numpy as np
import warnings

from qiskit import qasm3
from qiskit.exceptions import ExperimentalWarning
from qiskit.circuit.library import RealAmplitudes

warnings.filterwarnings("ignore", category=ExperimentalWarning)

# This is a little more than 5k gates.
base = RealAmplitudes(27, reps=100, flatten=True)
base = base.assign_parameters(np.random.rand(base.num_parameters))

print("Current OpenQASM 3 time")
%timeit qasm3.dumps(base)
print("New OpenQASM 3 time")
%timeit qasm3.dumps_experimental(base)
Current OpenQASM 3 time
75.7 ms ± 870 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)

New OpenQASM 3 time
15.2 ms ± 782 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)

I expect new qasm3.dumps() will become faster if it can be independent completely from python.

@to24toro to24toro requested a review from a team as a code owner January 15, 2025 07:42
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Qiskit/terra-core

@to24toro to24toro self-assigned this Jan 15, 2025
@to24toro to24toro added Rust This PR or issue is related to Rust code in the repository performance labels Jan 15, 2025
@to24toro to24toro modified the milestone: 2.0.0 Jan 15, 2025
@1ucian0 1ucian0 added this to the 2.0.0 milestone Jan 30, 2025
@coveralls
Copy link

coveralls commented Feb 1, 2025

Pull Request Test Coverage Report for Build 13213543783

Details

  • 565 of 1033 (54.7%) changed or added relevant lines in 5 files are covered.
  • 9 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.4%) to 87.899%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/qasm3/src/lib.rs 14 18 77.78%
crates/qasm3/src/ast.rs 20 53 37.74%
crates/qasm3/src/exporter.rs 318 490 64.9%
crates/qasm3/src/printer.rs 207 466 44.42%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.73%
crates/qasm2/src/parse.rs 6 97.15%
Totals Coverage Status
Change from base Build 13204846880: -0.4%
Covered Lines: 79189
Relevant Lines: 90091

💛 - Coveralls

@to24toro to24toro changed the title [WIP] Initial Implementation of a Limited QASM3 Exporter in Rust Initial Implementation of a Limited QASM3 Exporter in Rust Feb 6, 2025
@to24toro
Copy link
Contributor Author

to24toro commented Feb 6, 2025

Currently, registers cannot be printed because it has depended on python yet. So Exporter prints qubits one by one.

OPENQASM 3.0;
include "stdgates.inc";
qubit _qubit0;
qubit _qubit1;
qubit _qubit2;
qubit _qubit3;
p(0.5) _qubit0;
x _qubit1;
y _qubit2;
z _qubit3;
h _qubit0;
s _qubit1;
sdg _qubit2;
t _qubit3;
tdg _qubit0;
sx _qubit1;
rx(0.5) _qubit2;
ry(0.5) _qubit3;
rz(0.5) _qubit0;
id _qubit1;
cx _qubit0, _qubit1;
cy _qubit1, _qubit2;
cz _qubit2, _qubit3;
rzz(0.5) _qubit0, _qubit3;
cp(0.5) _qubit1, _qubit2;
crx(0.5) _qubit2, _qubit3;
cry(0.5) _qubit3, _qubit0;
crz(0.5) _qubit0, _qubit1;
ch _qubit1, _qubit2;
swap _qubit2, _qubit3;
cp(0.5) _qubit1, _qubit3;
p(0.5) _qubit0;
ccx _qubit0, _qubit1, _qubit2;
cswap _qubit0, _qubit1, _qubit3;
cu(0.5, 0.5, 0.5, 0.5) _qubit0, _qubit1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port QASM3 exporter to rust
5 participants