Skip to content

Feat/unitary definitions - #45

Open
Katie1harrison wants to merge 36 commits into
openqasm:mainfrom
paragon-lab:feat/unitary_definitions
Open

Feat/unitary definitions#45
Katie1harrison wants to merge 36 commits into
openqasm:mainfrom
paragon-lab:feat/unitary_definitions

Conversation

@Katie1harrison

Copy link
Copy Markdown

Add Matrix Definitions for the Unitary Data Type

Summary

This PR extends the unitary data type so that a unitary can be defined using a matrix initializer.

Previously, unitary declarations supported:

unitary u;

This PR adds support for:

unitary u = {
    {1, 0},
    {0, 1}
};

Changes

ASTUnitaryNode

Updated ASTUnitaryNode to optionally store an ASTInitializerList representing the matrix used to define the unitary.

Added:

const ASTInitializerList *INL;

along with a constructor for initialized unitaries and methods for accessing the initializer list.

The existing constructors for uninitialized unitary declarations and unitary gate calls are preserved.

ASTBuilder

Added an overloaded CreateASTUnitaryNode that accepts an ASTInitializerList.

This allows the builder to construct both:

unitary u;

and:

unitary u = {
    {1, 0},
    {0, 1}
};

ASTProductionFactory

Added a production rule for constructing a unitary declaration containing an initializer list. The new production rule passes the parsed initializer list to ASTBuilder.

Parser Grammar

Added grammar support for:

unitary Identifier = {
    { ... },
    { ... }
};

The implementation reuses the existing InitializerList grammar rather than introducing a separate matrix AST type.

AST Representation

For example:

unitary u = {
    {1, 0},
    {0, 1}
};

is represented as:

ASTUnitaryNode
├── Identifier: u
└── ASTInitializerList
    ├── ASTExpressionNodeList: 1, 0
    └── ASTExpressionNodeList: 0, 1

Testing

Added/tested parsing of an initialized unitary using:

tests/src/unitary/unitary_creation.qasm

The parser successfully produces an ASTTypeUnitary declaration containing the expected InitializerList and matrix entries.

Not Included

This PR adds the syntax and AST representation for matrix-defined unitaries. It does not yet validate that the provided matrix is mathematically unitary.

Matrix shape, element type, and unitarity validation can be handled later during semantic analysis/compiler validation.

Katie1harrison and others added 30 commits June 11, 2026 17:23
Older flake8 4.0.1 crashes under Python 3.12's importlib.metadata.
Also apply small pre-commit whitespace/newline fixes and sync uv.lock.

Co-authored-by: Cursor <cursoragent@cursor.com>
Parse snap([θ…])-style calls as a single classical ASTAngleArray
argument, and move fork-local production rules into the 10000+ range
to avoid colliding with upstream numbering.

Co-authored-by: Cursor <cursoragent@cursor.com>
added  comments explaining how tokens are created
changed ASTTypeQubit to ASTTypQumode for Qumode token
Qumode (and mixed qubit/qumode) call-site args resolve and validate like qubits, with opaque snap/ecd/disp stubs for CV gates.

Co-authored-by: Cursor <cursoragent@cursor.com>
Register TOK_DISP / ASTDispGateNode, accept a complex parameter and qumode
target, and allow leading qubit controls only under ctrl/negctrl. Include
related tests and the preprocessor full-file read fix.

Co-authored-by: Cursor <cursoragent@cursor.com>
Add builtin disp gate and qumode CV gate call support
Support explicit classical formals (angle/float/complex and arrays), typed
qubit/qumode operands, GateType compatibility (including array sizes), mixed
array+scalar call args, and angle-array body indexing across gates.

Co-authored-by: Cursor <cursoragent@cursor.com>
GateEOp construction was appending the bare target while the modifier
statement was also appended; skip or pop the target so only the wrapped
op remains in gate bodies.

Co-authored-by: Cursor <cursoragent@cursor.com>
Emit FullyTyped/FormalParamTypes/FormalQuantumTypes from ASTGateNode::print,
and encode complex/array formals plus qubit vs qumode in definition mangled
names (remangle after Formal* is attached).

Co-authored-by: Cursor <cursoragent@cursor.com>
Rename quantum carriers to Operands/OperandParams and GateOperandParam
(keeping mangler tag GQP), materialize qumode formals as ASTQumodeNode,
and attach BinaryOp/UnaryOp trees on disp complex Params instead of
folding through angle→real+0i.

Co-authored-by: Cursor <cursoragent@cursor.com>
Real literals and expressions against complex formals (and builtin disp) become MPComplex directly instead of angle coercion; document call-site GateQOpList as a representation quirk.

Co-authored-by: Cursor <cursoragent@cursor.com>
Support gate foo[uint N](array[T, N] …) with inferred or explicit N at
calls, and move array-initialization notes to GitHub issue #11.

Co-authored-by: Cursor <cursoragent@cursor.com>
Introduce Fock control distinct from qubit ctrl(n), switch templates and
Fock levels to square brackets, and allow arithmetic in ctrl[…] brackets.

Co-authored-by: Cursor <cursoragent@cursor.com>
joshmtlau and others added 4 commits August 3, 2026 16:57
Replace opaque SNAP with a typed cvgates definition, keep induction
indices and template bindings intact for lowering, and drop a local
issue draft already filed as GitHub #15.

Co-authored-by: Cursor <cursoragent@cursor.com>
Store float/mpdecimal (and call-site conversions) in Params as declared types,
use angle arrays for SNAP, and document the stable disp/snap/ecd call contract
plus project stable vs WIP split for compiler consumers.

Co-authored-by: Cursor <cursoragent@cursor.com>
…um-params

Typed gate declarations with fixed numbers of parameters
unitary u = {
    {1, 0},
    {0, 1}
};

semantic analysis will need to check

are all rows the same length?
is the matrix square?
are the entries valid numeric/complex expressions?

does it satisfy U†U=I?
@Katie1harrison
Katie1harrison requested a review from a team as a code owner August 7, 2026 21:23
@CLAassistant

CLAassistant commented Aug 7, 2026

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ joshmtlau
❌ Katie1harrison
You have signed the CLA already but the status is still pending? Let us recheck it.

@blakejohnson

Copy link
Copy Markdown
Contributor

Hi @Katie1harrison: a quick heads up that this project is no longer actively maintained by the original authors. You are welcome to fork this for your own use, or if you want to become a maintainer of the repo in its currently location, we would consider that.

I'm curious if this unitary type is defined somewhere? It is not formally part of the OpenQASM spec. There was some discussion a few years ago about adding Unitary subroutines, but the related spec change has been waiting a strong use case to push it over the finish line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants