Skip to content

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 14531773167

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 1167 of 1757 (66.42%) changed or added relevant lines in 7 files are covered.
  • 1138 unchanged lines in 30 files lost coverage.
  • Overall coverage decreased (-0.3%) to 87.794%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/qasm3/init.py 12 18 66.67%
crates/qasm3/src/ast.rs 20 53 37.74%
crates/qasm3/src/lib.rs 51 111 45.95%
crates/qasm3/src/printer.rs 297 509 58.35%
crates/qasm3/src/exporter.rs 782 1061 73.7%
Files with Coverage Reduction New Missed Lines %
crates/circuit/src/duration.rs 1 74.29%
qiskit/circuit/controlledgate.py 1 94.62%
qiskit/circuit/library/quantum_volume.py 1 93.18%
qiskit/synthesis/arithmetic/adders/draper_qft_adder.py 1 96.15%
crates/accelerate/src/sabre/layout.rs 2 74.5%
crates/qasm2/src/lex.rs 2 91.48%
qiskit/primitives/containers/data_bin.py 2 96.15%
qiskit/circuit/_add_control.py 3 97.84%
qiskit/result/models.py 4 92.77%
qiskit/synthesis/multi_controlled/mcx_synthesis.py 4 98.54%
Totals Coverage Status
Change from base Build 14068427467: -0.3%
Covered Lines: 74963
Relevant Lines: 85385

💛 - 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;

@1ucian0 1ucian0 modified the milestones: 2.0.0, 2.1.0 Feb 27, 2025
@Cryoris
Copy link
Contributor

Cryoris commented Mar 20, 2025

Registers are now also in Rust, so this block should no longer exist 🙂

@to24toro
Copy link
Contributor Author

to24toro commented Apr 21, 2025

Close and reopen at #14226

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
6 participants