unitaryguard 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unitaryguard-0.2.0/LICENSE +202 -0
- unitaryguard-0.2.0/NOTICE +8 -0
- unitaryguard-0.2.0/PKG-INFO +173 -0
- unitaryguard-0.2.0/README.md +155 -0
- unitaryguard-0.2.0/pyproject.toml +31 -0
- unitaryguard-0.2.0/setup.cfg +4 -0
- unitaryguard-0.2.0/tests/test_case_study_autoq_qec_guard.py +109 -0
- unitaryguard-0.2.0/tests/test_case_study_autoq_qec_guard_multiqubit.py +97 -0
- unitaryguard-0.2.0/tests/test_case_study_autoq_qec_guard_via_ket.py +147 -0
- unitaryguard-0.2.0/tests/test_case_study_ket_backends.py +136 -0
- unitaryguard-0.2.0/tests/test_case_study_rsgridsynth.py +87 -0
- unitaryguard-0.2.0/tests/test_cli_calibrate.py +27 -0
- unitaryguard-0.2.0/tests/test_cross_validate_ket.py +131 -0
- unitaryguard-0.2.0/tests/test_cross_validate_qiskit.py +162 -0
- unitaryguard-0.2.0/tests/test_exhaustive.py +56 -0
- unitaryguard-0.2.0/tests/test_known_bugs.py +80 -0
- unitaryguard-0.2.0/tests/test_parallel.py +49 -0
- unitaryguard-0.2.0/unitaryguard/__init__.py +25 -0
- unitaryguard-0.2.0/unitaryguard/cli.py +171 -0
- unitaryguard-0.2.0/unitaryguard/core.py +255 -0
- unitaryguard-0.2.0/unitaryguard/exhaustive.py +114 -0
- unitaryguard-0.2.0/unitaryguard/matrices.py +341 -0
- unitaryguard-0.2.0/unitaryguard/parallel.py +186 -0
- unitaryguard-0.2.0/unitaryguard.egg-info/PKG-INFO +173 -0
- unitaryguard-0.2.0/unitaryguard.egg-info/SOURCES.txt +27 -0
- unitaryguard-0.2.0/unitaryguard.egg-info/dependency_links.txt +1 -0
- unitaryguard-0.2.0/unitaryguard.egg-info/entry_points.txt +2 -0
- unitaryguard-0.2.0/unitaryguard.egg-info/requires.txt +1 -0
- unitaryguard-0.2.0/unitaryguard.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
UnitaryGuard
|
|
2
|
+
Copyright 2026 Ronaldo Rodrigues
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: unitaryguard
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Property-based semantic-equivalence checking for quantum circuit transforms: sample circuits, verify the unitary is preserved, shrink failures to a minimal reproduction. Framework-independent (no Qiskit or other SDK dependency) -- see DESIGN.md.
|
|
5
|
+
Author: Ronaldo Rodrigues
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/Ronaldoengenhariadacomputacao/unitaryguard
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Topic :: Scientific/Engineering :: Physics
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
License-File: NOTICE
|
|
16
|
+
Requires-Dist: numpy>=1.24
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# UnitaryGuard
|
|
20
|
+
|
|
21
|
+
Property-based semantic-equivalence checking for quantum circuit transforms.
|
|
22
|
+
**Framework-independent since v0.2** — no Qiskit or other SDK dependency
|
|
23
|
+
anywhere (see "Why v0.2 has no Qiskit dependency" below).
|
|
24
|
+
|
|
25
|
+
Points it at any `Circuit -> Circuit` function (a transform wrapping an
|
|
26
|
+
external engine, a compression step, anything), samples random circuits, and
|
|
27
|
+
checks one invariant that almost every transform is supposed to preserve but
|
|
28
|
+
almost no test suite checks directly: **does the output implement the same
|
|
29
|
+
unitary as the input?** When it finds a circuit where that's not true, it
|
|
30
|
+
automatically shrinks it to a small reproduction — the same thing you'd do
|
|
31
|
+
by hand while debugging, but for free.
|
|
32
|
+
|
|
33
|
+
## Why
|
|
34
|
+
|
|
35
|
+
This project exists because that exact manual process — sample circuits,
|
|
36
|
+
compare `Operator()` before/after a transform, shrink the failing case by
|
|
37
|
+
hand — found two real, silent correctness bugs in widely-used Clifford+T
|
|
38
|
+
tooling:
|
|
39
|
+
|
|
40
|
+
- Qiskit core's `OptimizeCliffordT` pass (merged fix:
|
|
41
|
+
[Qiskit/qiskit#16729](https://github.com/Qiskit/qiskit/pull/16729)).
|
|
42
|
+
- `rsgridsynth`'s `NormalForm` syllable-merge formula (open PR:
|
|
43
|
+
[qiskit-community/rsgridsynth#49](https://github.com/qiskit-community/rsgridsynth/pull/49)).
|
|
44
|
+
|
|
45
|
+
Both bugs had the same shape: the transform's gate-count/T-count output was
|
|
46
|
+
correct, while the circuit's actual unitary silently changed. Neither was
|
|
47
|
+
caught by T-count/gate-count/depth checks — only an explicit unitary
|
|
48
|
+
comparison catches this class of bug. UnitaryGuard is that check, packaged
|
|
49
|
+
so it doesn't have to be reinvented by hand each time. `tests/test_known_bugs.py`
|
|
50
|
+
reproduces both bug *shapes* as regression tests and proves the tool catches
|
|
51
|
+
them and shrinks each to a small reproduction.
|
|
52
|
+
|
|
53
|
+
See `DESIGN.md` for the full design rationale and current scope/limitations,
|
|
54
|
+
and `CASE_STUDIES.md` for every method used to find a real bug so far, each
|
|
55
|
+
with a runnable example (random sampling, exhaustive search, cross-validating
|
|
56
|
+
the tool's own oracle, and writing a native-language binding to reach code a
|
|
57
|
+
wrapper doesn't exercise).
|
|
58
|
+
|
|
59
|
+
## Why v0.2 has no Qiskit dependency
|
|
60
|
+
|
|
61
|
+
v0.1 checked equivalence via `qiskit.quantum_info.Operator`, which works
|
|
62
|
+
fine when the transform under test is unrelated to Qiskit -- but breaks
|
|
63
|
+
down exactly when the transform IS a real Qiskit pass: the code being
|
|
64
|
+
tested and the code judging it become the same library, so a shared bug in
|
|
65
|
+
Qiskit's own gate-matrix definitions can never be caught, structurally, no
|
|
66
|
+
matter how many circuits you sample. v0.2 replaces the Qiskit-based
|
|
67
|
+
`QuantumCircuit` representation with a tiny native `Circuit`/`Gate` pair and
|
|
68
|
+
a gate-matrix table (`unitaryguard/matrices.py`) derived directly from each
|
|
69
|
+
gate's standard mathematical definition, built with `numpy` alone. See
|
|
70
|
+
`DESIGN.md` ("v0.2: dropped the Qiskit dependency entirely") for the full
|
|
71
|
+
writeup. The real-world case that prompted this: validating AutoQ EngineBR,
|
|
72
|
+
an external, from-scratch transpiler -- see that project's own docs for a
|
|
73
|
+
worked adapter example (not included in this repo, since it's specific to
|
|
74
|
+
that engine's own wire protocol).
|
|
75
|
+
|
|
76
|
+
## Install
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pip install -e .
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Use as a library
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from unitaryguard import Circuit, Gate, CheckConfig, check_transform
|
|
86
|
+
|
|
87
|
+
def my_pass(circ: Circuit) -> Circuit:
|
|
88
|
+
... # your Circuit -> Circuit transform (e.g. call an external engine)
|
|
89
|
+
return transformed_circ
|
|
90
|
+
|
|
91
|
+
cfg = CheckConfig(
|
|
92
|
+
n_qubits=3,
|
|
93
|
+
gate_set=["h", "s", "sdg", "t", "tdg", "cx"],
|
|
94
|
+
n_samples=500,
|
|
95
|
+
seed=42,
|
|
96
|
+
)
|
|
97
|
+
report = check_transform(my_pass, cfg)
|
|
98
|
+
print(report.summary())
|
|
99
|
+
assert report.ok
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
`Circuit(n_qubits, gates)` and `Gate(kind, qubits, params=())` are plain
|
|
103
|
+
dataclasses -- `Gate("cx", (0, 1))`, `Gate("rz", (0,), (1.23,))`. See
|
|
104
|
+
`unitaryguard/matrices.py::GATE_TABLE` for the full supported vocabulary.
|
|
105
|
+
|
|
106
|
+
## Use from the CLI
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
unitaryguard check mymodule:my_pass --qubits 3 --gates h,s,sdg,t,tdg,cx --samples 500 --seed 42
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
`my_pass` must be an importable `Circuit -> Circuit` callable.
|
|
113
|
+
|
|
114
|
+
### Exhaustive mode (deterministic, targets gate-order/inversion bugs)
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
unitaryguard exhaustive mymodule:my_pass --qubits 1 --gates h,s,sdg,t,tdg --max-length 6
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Tests *every* discrete circuit up to `--max-length` gates (no continuous
|
|
121
|
+
parameters), shortest first. A gate-order/conjugation-formula transcription
|
|
122
|
+
bug (like both bugs this tool was built to generalize from) is
|
|
123
|
+
deterministic, not statistical -- exhaustive search guarantees finding the
|
|
124
|
+
smallest counterexample the vocabulary can express, with no separate
|
|
125
|
+
shrinking step needed.
|
|
126
|
+
|
|
127
|
+
Add `--workers N` to split each length's search across a process pool.
|
|
128
|
+
Defaults to 1 (sequential) unless you pass it explicitly.
|
|
129
|
+
|
|
130
|
+
**Measured, not assumed** -- on a 20-logical-core machine (Xeon E5-2666,
|
|
131
|
+
figures below are *specific to that machine*, not a universal constant):
|
|
132
|
+
parallelism only pays off once the workload is large enough to amortize
|
|
133
|
+
each worker's one-time Qiskit import cost (~1.4s/process). On a small
|
|
134
|
+
search (1555 circuits, max length 4), throughput peaked at `--workers 4`
|
|
135
|
+
(1.6x) and *degraded* past that. On a larger search (9331 circuits, max
|
|
136
|
+
length 5), `--workers 8` gave a real 3.3x speedup, plateauing (not
|
|
137
|
+
improving further) from 12 workers up to the machine's full 20.
|
|
138
|
+
|
|
139
|
+
The **principle** generalizes across hardware, the **numbers** don't --
|
|
140
|
+
if you're on a 4-core laptop, "8-12 workers" is meaningless (there's no 12
|
|
141
|
+
cores to use, and oversubscribing usually hurts, not helps). Re-run the
|
|
142
|
+
same small-vs-large comparison on your own machine before picking a
|
|
143
|
+
`--workers` value for anything you'll run repeatedly; as a starting point,
|
|
144
|
+
try `--workers <your core count>` and only go lower if you see the same
|
|
145
|
+
kind of degradation documented above for a small search.
|
|
146
|
+
|
|
147
|
+
## Current scope (v0.2)
|
|
148
|
+
|
|
149
|
+
- Transforms that preserve qubit count (most gate-level optimization/
|
|
150
|
+
synthesis passes). Ancilla-widening transforms are not yet supported —
|
|
151
|
+
see `DESIGN.md`.
|
|
152
|
+
- Unitary-only transforms — no mid-circuit measurement / classical control
|
|
153
|
+
yet.
|
|
154
|
+
- Gate vocabulary: `h,x,y,z,s,sdg,t,tdg,sx,sxdg` (0-param, 1-qubit),
|
|
155
|
+
`rz,ry,rx,p` (1-param, 1-qubit), `u` (3-param), `r` (2-param),
|
|
156
|
+
`cx,cz,swap,iswap,dcx,ecr,cy,ch,csx` (0-param, 2-qubit),
|
|
157
|
+
`crz,crx,cry,cp,rzz,rxx,ryy,rzx` (1-param, 2-qubit),
|
|
158
|
+
`cu,xx_plus_yy,xx_minus_yy` (multi-param, 2-qubit),
|
|
159
|
+
`ccx,cswap,ccz,rccx` (0-param, 3-qubit) — see
|
|
160
|
+
`unitaryguard/matrices.py::GATE_TABLE`. No generic arbitrary-unitary
|
|
161
|
+
sampling (`UnitaryGate`-equivalent) yet — extending vocabulary further is
|
|
162
|
+
a matter of adding matrix functions + table entries to `matrices.py`;
|
|
163
|
+
every entry added so far was cross-validated against two independent
|
|
164
|
+
oracles (Ket and Qiskit's `Operator()`) before being trusted — see
|
|
165
|
+
`CASE_STUDIES.md`.
|
|
166
|
+
- No longer wraps real Qiskit `PassManager`/pass objects directly (that
|
|
167
|
+
capability was removed in v0.2 along with the Qiskit dependency — see
|
|
168
|
+
"Why v0.2 has no Qiskit dependency" above).
|
|
169
|
+
|
|
170
|
+
## Status
|
|
171
|
+
|
|
172
|
+
Early prototype, private (not yet published to PyPI). v0.2 (2026-09-21):
|
|
173
|
+
dropped Qiskit dependency entirely, see DESIGN.md.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# UnitaryGuard
|
|
2
|
+
|
|
3
|
+
Property-based semantic-equivalence checking for quantum circuit transforms.
|
|
4
|
+
**Framework-independent since v0.2** — no Qiskit or other SDK dependency
|
|
5
|
+
anywhere (see "Why v0.2 has no Qiskit dependency" below).
|
|
6
|
+
|
|
7
|
+
Points it at any `Circuit -> Circuit` function (a transform wrapping an
|
|
8
|
+
external engine, a compression step, anything), samples random circuits, and
|
|
9
|
+
checks one invariant that almost every transform is supposed to preserve but
|
|
10
|
+
almost no test suite checks directly: **does the output implement the same
|
|
11
|
+
unitary as the input?** When it finds a circuit where that's not true, it
|
|
12
|
+
automatically shrinks it to a small reproduction — the same thing you'd do
|
|
13
|
+
by hand while debugging, but for free.
|
|
14
|
+
|
|
15
|
+
## Why
|
|
16
|
+
|
|
17
|
+
This project exists because that exact manual process — sample circuits,
|
|
18
|
+
compare `Operator()` before/after a transform, shrink the failing case by
|
|
19
|
+
hand — found two real, silent correctness bugs in widely-used Clifford+T
|
|
20
|
+
tooling:
|
|
21
|
+
|
|
22
|
+
- Qiskit core's `OptimizeCliffordT` pass (merged fix:
|
|
23
|
+
[Qiskit/qiskit#16729](https://github.com/Qiskit/qiskit/pull/16729)).
|
|
24
|
+
- `rsgridsynth`'s `NormalForm` syllable-merge formula (open PR:
|
|
25
|
+
[qiskit-community/rsgridsynth#49](https://github.com/qiskit-community/rsgridsynth/pull/49)).
|
|
26
|
+
|
|
27
|
+
Both bugs had the same shape: the transform's gate-count/T-count output was
|
|
28
|
+
correct, while the circuit's actual unitary silently changed. Neither was
|
|
29
|
+
caught by T-count/gate-count/depth checks — only an explicit unitary
|
|
30
|
+
comparison catches this class of bug. UnitaryGuard is that check, packaged
|
|
31
|
+
so it doesn't have to be reinvented by hand each time. `tests/test_known_bugs.py`
|
|
32
|
+
reproduces both bug *shapes* as regression tests and proves the tool catches
|
|
33
|
+
them and shrinks each to a small reproduction.
|
|
34
|
+
|
|
35
|
+
See `DESIGN.md` for the full design rationale and current scope/limitations,
|
|
36
|
+
and `CASE_STUDIES.md` for every method used to find a real bug so far, each
|
|
37
|
+
with a runnable example (random sampling, exhaustive search, cross-validating
|
|
38
|
+
the tool's own oracle, and writing a native-language binding to reach code a
|
|
39
|
+
wrapper doesn't exercise).
|
|
40
|
+
|
|
41
|
+
## Why v0.2 has no Qiskit dependency
|
|
42
|
+
|
|
43
|
+
v0.1 checked equivalence via `qiskit.quantum_info.Operator`, which works
|
|
44
|
+
fine when the transform under test is unrelated to Qiskit -- but breaks
|
|
45
|
+
down exactly when the transform IS a real Qiskit pass: the code being
|
|
46
|
+
tested and the code judging it become the same library, so a shared bug in
|
|
47
|
+
Qiskit's own gate-matrix definitions can never be caught, structurally, no
|
|
48
|
+
matter how many circuits you sample. v0.2 replaces the Qiskit-based
|
|
49
|
+
`QuantumCircuit` representation with a tiny native `Circuit`/`Gate` pair and
|
|
50
|
+
a gate-matrix table (`unitaryguard/matrices.py`) derived directly from each
|
|
51
|
+
gate's standard mathematical definition, built with `numpy` alone. See
|
|
52
|
+
`DESIGN.md` ("v0.2: dropped the Qiskit dependency entirely") for the full
|
|
53
|
+
writeup. The real-world case that prompted this: validating AutoQ EngineBR,
|
|
54
|
+
an external, from-scratch transpiler -- see that project's own docs for a
|
|
55
|
+
worked adapter example (not included in this repo, since it's specific to
|
|
56
|
+
that engine's own wire protocol).
|
|
57
|
+
|
|
58
|
+
## Install
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
pip install -e .
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Use as a library
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from unitaryguard import Circuit, Gate, CheckConfig, check_transform
|
|
68
|
+
|
|
69
|
+
def my_pass(circ: Circuit) -> Circuit:
|
|
70
|
+
... # your Circuit -> Circuit transform (e.g. call an external engine)
|
|
71
|
+
return transformed_circ
|
|
72
|
+
|
|
73
|
+
cfg = CheckConfig(
|
|
74
|
+
n_qubits=3,
|
|
75
|
+
gate_set=["h", "s", "sdg", "t", "tdg", "cx"],
|
|
76
|
+
n_samples=500,
|
|
77
|
+
seed=42,
|
|
78
|
+
)
|
|
79
|
+
report = check_transform(my_pass, cfg)
|
|
80
|
+
print(report.summary())
|
|
81
|
+
assert report.ok
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
`Circuit(n_qubits, gates)` and `Gate(kind, qubits, params=())` are plain
|
|
85
|
+
dataclasses -- `Gate("cx", (0, 1))`, `Gate("rz", (0,), (1.23,))`. See
|
|
86
|
+
`unitaryguard/matrices.py::GATE_TABLE` for the full supported vocabulary.
|
|
87
|
+
|
|
88
|
+
## Use from the CLI
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
unitaryguard check mymodule:my_pass --qubits 3 --gates h,s,sdg,t,tdg,cx --samples 500 --seed 42
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
`my_pass` must be an importable `Circuit -> Circuit` callable.
|
|
95
|
+
|
|
96
|
+
### Exhaustive mode (deterministic, targets gate-order/inversion bugs)
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
unitaryguard exhaustive mymodule:my_pass --qubits 1 --gates h,s,sdg,t,tdg --max-length 6
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Tests *every* discrete circuit up to `--max-length` gates (no continuous
|
|
103
|
+
parameters), shortest first. A gate-order/conjugation-formula transcription
|
|
104
|
+
bug (like both bugs this tool was built to generalize from) is
|
|
105
|
+
deterministic, not statistical -- exhaustive search guarantees finding the
|
|
106
|
+
smallest counterexample the vocabulary can express, with no separate
|
|
107
|
+
shrinking step needed.
|
|
108
|
+
|
|
109
|
+
Add `--workers N` to split each length's search across a process pool.
|
|
110
|
+
Defaults to 1 (sequential) unless you pass it explicitly.
|
|
111
|
+
|
|
112
|
+
**Measured, not assumed** -- on a 20-logical-core machine (Xeon E5-2666,
|
|
113
|
+
figures below are *specific to that machine*, not a universal constant):
|
|
114
|
+
parallelism only pays off once the workload is large enough to amortize
|
|
115
|
+
each worker's one-time Qiskit import cost (~1.4s/process). On a small
|
|
116
|
+
search (1555 circuits, max length 4), throughput peaked at `--workers 4`
|
|
117
|
+
(1.6x) and *degraded* past that. On a larger search (9331 circuits, max
|
|
118
|
+
length 5), `--workers 8` gave a real 3.3x speedup, plateauing (not
|
|
119
|
+
improving further) from 12 workers up to the machine's full 20.
|
|
120
|
+
|
|
121
|
+
The **principle** generalizes across hardware, the **numbers** don't --
|
|
122
|
+
if you're on a 4-core laptop, "8-12 workers" is meaningless (there's no 12
|
|
123
|
+
cores to use, and oversubscribing usually hurts, not helps). Re-run the
|
|
124
|
+
same small-vs-large comparison on your own machine before picking a
|
|
125
|
+
`--workers` value for anything you'll run repeatedly; as a starting point,
|
|
126
|
+
try `--workers <your core count>` and only go lower if you see the same
|
|
127
|
+
kind of degradation documented above for a small search.
|
|
128
|
+
|
|
129
|
+
## Current scope (v0.2)
|
|
130
|
+
|
|
131
|
+
- Transforms that preserve qubit count (most gate-level optimization/
|
|
132
|
+
synthesis passes). Ancilla-widening transforms are not yet supported —
|
|
133
|
+
see `DESIGN.md`.
|
|
134
|
+
- Unitary-only transforms — no mid-circuit measurement / classical control
|
|
135
|
+
yet.
|
|
136
|
+
- Gate vocabulary: `h,x,y,z,s,sdg,t,tdg,sx,sxdg` (0-param, 1-qubit),
|
|
137
|
+
`rz,ry,rx,p` (1-param, 1-qubit), `u` (3-param), `r` (2-param),
|
|
138
|
+
`cx,cz,swap,iswap,dcx,ecr,cy,ch,csx` (0-param, 2-qubit),
|
|
139
|
+
`crz,crx,cry,cp,rzz,rxx,ryy,rzx` (1-param, 2-qubit),
|
|
140
|
+
`cu,xx_plus_yy,xx_minus_yy` (multi-param, 2-qubit),
|
|
141
|
+
`ccx,cswap,ccz,rccx` (0-param, 3-qubit) — see
|
|
142
|
+
`unitaryguard/matrices.py::GATE_TABLE`. No generic arbitrary-unitary
|
|
143
|
+
sampling (`UnitaryGate`-equivalent) yet — extending vocabulary further is
|
|
144
|
+
a matter of adding matrix functions + table entries to `matrices.py`;
|
|
145
|
+
every entry added so far was cross-validated against two independent
|
|
146
|
+
oracles (Ket and Qiskit's `Operator()`) before being trusted — see
|
|
147
|
+
`CASE_STUDIES.md`.
|
|
148
|
+
- No longer wraps real Qiskit `PassManager`/pass objects directly (that
|
|
149
|
+
capability was removed in v0.2 along with the Qiskit dependency — see
|
|
150
|
+
"Why v0.2 has no Qiskit dependency" above).
|
|
151
|
+
|
|
152
|
+
## Status
|
|
153
|
+
|
|
154
|
+
Early prototype, private (not yet published to PyPI). v0.2 (2026-09-21):
|
|
155
|
+
dropped Qiskit dependency entirely, see DESIGN.md.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "unitaryguard"
|
|
7
|
+
version = "0.2.0"
|
|
8
|
+
description = "Property-based semantic-equivalence checking for quantum circuit transforms: sample circuits, verify the unitary is preserved, shrink failures to a minimal reproduction. Framework-independent (no Qiskit or other SDK dependency) -- see DESIGN.md."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
license-files = ["LICENSE", "NOTICE"]
|
|
13
|
+
authors = [{ name = "Ronaldo Rodrigues" }]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Science/Research",
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Topic :: Scientific/Engineering :: Physics",
|
|
19
|
+
]
|
|
20
|
+
dependencies = [
|
|
21
|
+
"numpy>=1.24",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Repository = "https://github.com/Ronaldoengenhariadacomputacao/unitaryguard"
|
|
26
|
+
|
|
27
|
+
[project.scripts]
|
|
28
|
+
unitaryguard = "unitaryguard.cli:main"
|
|
29
|
+
|
|
30
|
+
[tool.setuptools.packages.find]
|
|
31
|
+
include = ["unitaryguard*"]
|