pyzx-param 0.9.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.
Files changed (106) hide show
  1. pyzx_param-0.9.0/LICENSE +201 -0
  2. pyzx_param-0.9.0/MANIFEST.in +9 -0
  3. pyzx_param-0.9.0/PKG-INFO +124 -0
  4. pyzx_param-0.9.0/pyzx_param/__init__.py +66 -0
  5. pyzx_param-0.9.0/pyzx_param/__main__.py +26 -0
  6. pyzx_param-0.9.0/pyzx_param/altextract.py +250 -0
  7. pyzx_param-0.9.0/pyzx_param/circuit/__init__.py +548 -0
  8. pyzx_param-0.9.0/pyzx_param/circuit/emojiparser.py +45 -0
  9. pyzx_param-0.9.0/pyzx_param/circuit/gates.py +1342 -0
  10. pyzx_param-0.9.0/pyzx_param/circuit/graphparser.py +158 -0
  11. pyzx_param-0.9.0/pyzx_param/circuit/qasmparser.py +268 -0
  12. pyzx_param-0.9.0/pyzx_param/circuit/qcparser.py +109 -0
  13. pyzx_param-0.9.0/pyzx_param/circuit/qsimparser.py +60 -0
  14. pyzx_param-0.9.0/pyzx_param/circuit/quipperparser.py +133 -0
  15. pyzx_param-0.9.0/pyzx_param/circuit/sqasm.py +75 -0
  16. pyzx_param-0.9.0/pyzx_param/css.py +57 -0
  17. pyzx_param-0.9.0/pyzx_param/d3.py +21 -0
  18. pyzx_param-0.9.0/pyzx_param/drawing.py +771 -0
  19. pyzx_param-0.9.0/pyzx_param/editor.py +498 -0
  20. pyzx_param-0.9.0/pyzx_param/editor_actions.py +496 -0
  21. pyzx_param-0.9.0/pyzx_param/extract.py +1477 -0
  22. pyzx_param-0.9.0/pyzx_param/fourier.py +110 -0
  23. pyzx_param-0.9.0/pyzx_param/gadget_extract.py +115 -0
  24. pyzx_param-0.9.0/pyzx_param/generate.py +740 -0
  25. pyzx_param-0.9.0/pyzx_param/gflow.py +145 -0
  26. pyzx_param-0.9.0/pyzx_param/graph/__init__.py +31 -0
  27. pyzx_param-0.9.0/pyzx_param/graph/base.py +1096 -0
  28. pyzx_param-0.9.0/pyzx_param/graph/diff.py +195 -0
  29. pyzx_param-0.9.0/pyzx_param/graph/graph.py +68 -0
  30. pyzx_param-0.9.0/pyzx_param/graph/graph_gt.py +130 -0
  31. pyzx_param-0.9.0/pyzx_param/graph/graph_ig.py +185 -0
  32. pyzx_param-0.9.0/pyzx_param/graph/graph_s.py +432 -0
  33. pyzx_param-0.9.0/pyzx_param/graph/jsonparser.py +444 -0
  34. pyzx_param-0.9.0/pyzx_param/graph/multigraph.py +492 -0
  35. pyzx_param-0.9.0/pyzx_param/graph/scalar.py +480 -0
  36. pyzx_param-0.9.0/pyzx_param/hsimplify.py +235 -0
  37. pyzx_param-0.9.0/pyzx_param/js/d3.v5.min.js +2 -0
  38. pyzx_param-0.9.0/pyzx_param/js/zx_editor_model.js +163 -0
  39. pyzx_param-0.9.0/pyzx_param/js/zx_editor_widget.js +531 -0
  40. pyzx_param-0.9.0/pyzx_param/js/zx_viewer.inline.js +370 -0
  41. pyzx_param-0.9.0/pyzx_param/js/zx_viewer.js +381 -0
  42. pyzx_param-0.9.0/pyzx_param/js/zx_viewer3d.js +275 -0
  43. pyzx_param-0.9.0/pyzx_param/linalg.py +485 -0
  44. pyzx_param-0.9.0/pyzx_param/local_search/__init__.py +15 -0
  45. pyzx_param-0.9.0/pyzx_param/local_search/congruences.py +272 -0
  46. pyzx_param-0.9.0/pyzx_param/local_search/genetic.py +249 -0
  47. pyzx_param-0.9.0/pyzx_param/local_search/scores.py +63 -0
  48. pyzx_param-0.9.0/pyzx_param/local_search/simulated_annealing.py +107 -0
  49. pyzx_param-0.9.0/pyzx_param/mbqc.py +99 -0
  50. pyzx_param-0.9.0/pyzx_param/optimize.py +778 -0
  51. pyzx_param-0.9.0/pyzx_param/parity_network.py +52 -0
  52. pyzx_param-0.9.0/pyzx_param/pauliweb.py +259 -0
  53. pyzx_param-0.9.0/pyzx_param/py.typed +0 -0
  54. pyzx_param-0.9.0/pyzx_param/pyquil_circuit.py +149 -0
  55. pyzx_param-0.9.0/pyzx_param/quimb.py +106 -0
  56. pyzx_param-0.9.0/pyzx_param/rank_width.py +462 -0
  57. pyzx_param-0.9.0/pyzx_param/rewrite.py +151 -0
  58. pyzx_param-0.9.0/pyzx_param/rewrite_rules/__init__.py +15 -0
  59. pyzx_param-0.9.0/pyzx_param/rewrite_rules/basicrules.py +265 -0
  60. pyzx_param-0.9.0/pyzx_param/rewrite_rules/editor_actions.py +498 -0
  61. pyzx_param-0.9.0/pyzx_param/rewrite_rules/hrules.py +647 -0
  62. pyzx_param-0.9.0/pyzx_param/rewrite_rules/rules.py +1291 -0
  63. pyzx_param-0.9.0/pyzx_param/routing/__init__.py +52 -0
  64. pyzx_param-0.9.0/pyzx_param/routing/architecture.py +1170 -0
  65. pyzx_param-0.9.0/pyzx_param/routing/cnot_mapper.py +513 -0
  66. pyzx_param-0.9.0/pyzx_param/routing/machine_learning.py +487 -0
  67. pyzx_param-0.9.0/pyzx_param/routing/parity_maps.py +232 -0
  68. pyzx_param-0.9.0/pyzx_param/routing/phase_poly.py +1396 -0
  69. pyzx_param-0.9.0/pyzx_param/routing/steiner.py +376 -0
  70. pyzx_param-0.9.0/pyzx_param/scripts/__init__.py +60 -0
  71. pyzx_param-0.9.0/pyzx_param/scripts/circ2circ.py +100 -0
  72. pyzx_param-0.9.0/pyzx_param/scripts/circ2tikz.py +62 -0
  73. pyzx_param-0.9.0/pyzx_param/scripts/circuit_router.py +1317 -0
  74. pyzx_param-0.9.0/pyzx_param/scripts/cnot_generator.py +82 -0
  75. pyzx_param-0.9.0/pyzx_param/scripts/phase_poly_generator.py +120 -0
  76. pyzx_param-0.9.0/pyzx_param/simplify.py +765 -0
  77. pyzx_param-0.9.0/pyzx_param/simulate.py +1051 -0
  78. pyzx_param-0.9.0/pyzx_param/symbolic.py +386 -0
  79. pyzx_param-0.9.0/pyzx_param/tensor.py +330 -0
  80. pyzx_param-0.9.0/pyzx_param/tikz.py +409 -0
  81. pyzx_param-0.9.0/pyzx_param/todd.py +548 -0
  82. pyzx_param-0.9.0/pyzx_param/utils.py +311 -0
  83. pyzx_param-0.9.0/pyzx_param.egg-info/PKG-INFO +124 -0
  84. pyzx_param-0.9.0/pyzx_param.egg-info/SOURCES.txt +104 -0
  85. pyzx_param-0.9.0/pyzx_param.egg-info/dependency_links.txt +1 -0
  86. pyzx_param-0.9.0/pyzx_param.egg-info/requires.txt +5 -0
  87. pyzx_param-0.9.0/pyzx_param.egg-info/top_level.txt +1 -0
  88. pyzx_param-0.9.0/readme.md +88 -0
  89. pyzx_param-0.9.0/setup.cfg +4 -0
  90. pyzx_param-0.9.0/setup.py +45 -0
  91. pyzx_param-0.9.0/tests/test_circuit.py +147 -0
  92. pyzx_param-0.9.0/tests/test_cnot_mapper.py +404 -0
  93. pyzx_param-0.9.0/tests/test_dyadic.py +68 -0
  94. pyzx_param-0.9.0/tests/test_extract.py +107 -0
  95. pyzx_param-0.9.0/tests/test_graph.py +248 -0
  96. pyzx_param-0.9.0/tests/test_jsonparser.py +327 -0
  97. pyzx_param-0.9.0/tests/test_linalg.py +102 -0
  98. pyzx_param-0.9.0/tests/test_phasepoly.py +316 -0
  99. pyzx_param-0.9.0/tests/test_qasm.py +364 -0
  100. pyzx_param-0.9.0/tests/test_quimb.py +132 -0
  101. pyzx_param-0.9.0/tests/test_rank_width.py +125 -0
  102. pyzx_param-0.9.0/tests/test_scalar.py +78 -0
  103. pyzx_param-0.9.0/tests/test_scripts.py +68 -0
  104. pyzx_param-0.9.0/tests/test_simplify.py +286 -0
  105. pyzx_param-0.9.0/tests/test_simulate.py +176 -0
  106. pyzx_param-0.9.0/tests/test_tensor.py +186 -0
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,9 @@
1
+ include pyzx_param/js/zx_editor_model.js
2
+ include pyzx_param/js/zx_editor_widget.js
3
+ include pyzx_param/js/zx_viewer.js
4
+ include pyzx_param/js/d3.v5.min.js
5
+ include pyzx_param/js/zx_viewer.inline.js
6
+ include pyzx_param/js/zx_viewer3d.js
7
+ include pyzx_param/py.typed
8
+ include readme.md
9
+ include LICENSE
@@ -0,0 +1,124 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyzx-param
3
+ Version: 0.9.0
4
+ Summary: Library for quantum circuit rewriting and optimisation using the ZX-calculus
5
+ Home-page: https://github.com/rafaelha/pyzx.git
6
+ Author: Quantomatic
7
+ Author-email: john@vdwetering.name
8
+ License: Apache2
9
+ Classifier: License :: OSI Approved :: Apache Software License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.9
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
16
+ Classifier: Development Status :: 4 - Beta
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: typing_extensions>=3.7.4
21
+ Requires-Dist: numpy>=1.14
22
+ Requires-Dist: pyperclip>=1.8.1
23
+ Requires-Dist: tqdm>=4.56.0
24
+ Requires-Dist: lark>=1.2.2
25
+ Dynamic: author
26
+ Dynamic: author-email
27
+ Dynamic: classifier
28
+ Dynamic: description
29
+ Dynamic: description-content-type
30
+ Dynamic: home-page
31
+ Dynamic: license
32
+ Dynamic: license-file
33
+ Dynamic: requires-dist
34
+ Dynamic: requires-python
35
+ Dynamic: summary
36
+
37
+ ## PyZX-Param
38
+
39
+ [![PyPI version](https://badge.fury.io/py/pyzx-param.svg)](https://badge.fury.io/py/pyzx-param)
40
+
41
+ > **Note:** This is a fork of [PyZX](https://github.com/zxcalc/pyzx) with extensions for parameterized Pauli vertices, based on [mjsutcliffe99/ParamZX](https://github.com/mjsutcliffe99/ParamZX). It can be installed alongside the original PyZX without conflicts.
42
+
43
+ PyZX (pronounce as *Pisics*) is a **Py**thon tool implementing the theory of **ZX**-calculus for the creation, visualisation, and automated rewriting of large-scale quantum circuits. Please watch [this 2-minute video](https://www.youtube.com/watch?v=iC-KVdB8pf0) for a short introduction.
44
+
45
+ PyZX currently allows you to:
46
+
47
+ * Read in quantum circuits in the file format of [QASM](https://en.wikipedia.org/wiki/OpenQASM), [Quipper](https://www.mathstat.dal.ca/~selinger/quipper/doc/) or [Quantomatic](https://quantomatic.github.io/).
48
+ * Rewrite circuits into a pseudo-normal form using the ZX-calculus.
49
+ * Extract new simplified circuits from these reduced graphs.
50
+ * Visualise the ZX-graphs and rewrites using either [Matplotlib](https://matplotlib.org/), Quantomatic or as a TikZ file for use in LaTeX documents.
51
+ * Output the optimised circuits in QASM, QC or QUIPPER format.
52
+
53
+ You can try out the in-browser demo which shows some of these features [here](http://zxcalculus.com/pyzx.html).
54
+
55
+ ## About the ZX-calculus
56
+
57
+ ZX-diagrams are a type of tensor network built out of combinations of linear maps known as *spiders*. There are 2 types of spiders: the Z-spiders (represented as green dots in PyZX) and the X-spiders (represented as red dots). Every linear map between some set of qubits can be represented by a ZX-diagram.
58
+ The ZX-calculus is a set of rewrite rules for ZX-diagrams. There are various extensive set of rewrite rules. PyZX however, uses only rewrite rules concerning the Clifford fragment of the ZX-calculus. Importantly, this set of rewrite rules is *complete* for Clifford diagrams, meaning that two representations of a Clifford map can be rewritten into one another if and only if the two linear maps they represent are equal.
59
+
60
+ [Here](http://zxcalculus.com) is a website with resources and information about the ZX-calculus. For a short introduction to the ZX-calculus see [this paper](https://arxiv.org/abs/1602.04744) while for a complete overview we recommend [this book](https://www.amazon.com/Picturing-Quantum-Processes-Diagrammatic-Reasoning/dp/110710422X). PyZX extensively uses two derived rewrite rules known as *local complementation* and *pivoting*. More information about these operations can be found in [this paper](https://arxiv.org/abs/1307.7048).
61
+
62
+
63
+ ## Installation
64
+
65
+ If you wish to use PyZX-Param as a Python module for use in other projects, we recommend installing via pip:
66
+ ```
67
+ pip install pyzx-param
68
+ ```
69
+
70
+ This installs the module as `pyzx_param`, allowing it to coexist with the original `pyzx`:
71
+ ```python
72
+ import pyzx_param as zx # This fork
73
+ import pyzx # Original PyZX (if needed)
74
+ ```
75
+
76
+ If you want to use the demos or the benchmark circuits you should install PyZX-Param from source by cloning the git repository.
77
+
78
+ PyZX-Param has no strict dependencies, although some functionality requires numpy. It is built to interact well with Jupyter, so we additionally recommend you have Jupyter and matplotlib installed.
79
+
80
+ ## Usage
81
+
82
+ See the [Documentation](https://pyzx.readthedocs.io/en/latest/) for a full overview of the features of PyZX.
83
+
84
+ If you have [Jupyter](https://jupyter.org/) installed you can use one of the demonstration notebooks in the demos folder for an illustration of what PyZX can do.
85
+
86
+ This is some example Python code for generating a random circuit, optimizing it, and finally displaying it:
87
+
88
+ ```python
89
+ import pyzx_param as zx
90
+ qubit_amount = 5
91
+ gate_count = 80
92
+ #Generate random circuit of Clifford gates
93
+ circuit = zx.generate.cliffordT(qubit_amount, gate_count)
94
+ #If running in Jupyter, draw the circuit
95
+ zx.draw(circuit)
96
+ #Use one of the built-in rewriting strategies to simplify the circuit
97
+ zx.simplify.full_reduce(circuit)
98
+ #See the result
99
+ zx.draw(circuit)
100
+ ```
101
+
102
+ PyZX-Param can also be run from the commandline. To optimise a circuit you can for instance run
103
+ ```python -m pyzx_param opt input_circuit.qasm```
104
+
105
+ ## Attribution
106
+
107
+ This is a fork of [PyZX](https://github.com/zxcalc/pyzx) by Aleks Kissinger and John van de Wetering. If you wish to cite PyZX in an academic work, please cite the [original paper](https://arxiv.org/abs/1904.04735):
108
+ <pre>
109
+ @inproceedings{kissinger2020Pyzx,
110
+ author = {Kissinger, Aleks and van de Wetering, John},
111
+ title = {{PyZX: Large Scale Automated Diagrammatic Reasoning}},
112
+ year = {2020},
113
+ booktitle = {{\rm Proceedings 16th International Conference on} Quantum Physics and Logic, {\rm Chapman University, Orange, CA, USA., 10-14 June 2019}},
114
+ editor = {Coecke, Bob and Leifer, Matthew},
115
+ series = {Electronic Proceedings in Theoretical Computer Science},
116
+ volume = {318},
117
+ pages = {229-241},
118
+ publisher = {Open Publishing Association},
119
+ doi = {10.4204/EPTCS.318.14}
120
+ }
121
+ </pre>
122
+
123
+ Here's a plane that says PYZX:
124
+ ![PYZX](https://github.com/zxcalc/pyzx/raw/master/images/F-PYZX.jpg)
@@ -0,0 +1,66 @@
1
+ # PyZX - Python library for quantum circuit rewriting
2
+ # and optimisation using the ZX-calculus
3
+ # Copyright (C) 2018 - Aleks Kissinger and John van de Wetering
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ __version__ = "0.9.0"
18
+
19
+ from .graph.graph import Graph
20
+ from .circuit import Circuit, gates, id
21
+ from .linalg import Mat2
22
+ from .utils import settings, VertexType, EdgeType, original_colors, rgb_colors, grayscale_colors
23
+ from .quimb import to_quimb_tensor
24
+ from .drawing import *
25
+ from .simplify import *
26
+ from .optimize import *
27
+ from .extract import *
28
+ from .graph.jsonparser import *
29
+ from .tensor import *
30
+ from .local_search.simulated_annealing import anneal
31
+ from .local_search.genetic import GeneticOptimizer
32
+ from .circuit.qasmparser import qasm
33
+ from .circuit.sqasm import sqasm
34
+ from . import generate
35
+ from . import todd
36
+ from . import linalg
37
+ from . import extract
38
+ from .rewrite_rules import rules
39
+ from .rewrite_rules import hrules
40
+ from .rewrite_rules import basicrules
41
+ from .rewrite_rules import editor_actions
42
+ from . import optimize
43
+ from . import simplify
44
+ from . import hsimplify
45
+ from . import d3
46
+ from . import tikz
47
+ from . import simulate
48
+ from . import editor
49
+ from . import routing
50
+ from . import local_search
51
+ from .routing.parity_maps import CNOT_tracker
52
+
53
+ # some common scalars
54
+ from .graph.base import Scalar
55
+ ONE = Scalar()
56
+ SQRT_TWO = Scalar()
57
+ SQRT_TWO.add_power(1)
58
+ TWO = Scalar()
59
+ TWO.add_power(2)
60
+ SQRT_TWO_INV = Scalar()
61
+ SQRT_TWO_INV.add_power(-1)
62
+ TWO_INV = Scalar()
63
+ TWO_INV.add_power(-2)
64
+
65
+ if __name__ == '__main__':
66
+ print("Please execute this as a module by running 'python -m pyzx'")
@@ -0,0 +1,26 @@
1
+ # PyZX - Python library for quantum circuit rewriting
2
+ # and optimization using the ZX-calculus
3
+ # Copyright (C) 2018 - Aleks Kissinger and John van de Wetering
4
+
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ if __name__ == '__main__':
19
+ import sys
20
+ try:
21
+ from .scripts import main
22
+ except (SystemError, ImportError):
23
+ print("Please run as a module by using 'python -m pyzx'")
24
+ exit(1)
25
+
26
+ main(sys.argv)
@@ -0,0 +1,250 @@
1
+ from fractions import Fraction
2
+ import itertools
3
+ import math
4
+
5
+ from .utils import EdgeType, VertexType, toggle_edge
6
+ from .linalg import Mat2, Z2
7
+ from .simplify import id_simp, tcount
8
+ from .rewrite_rules.rules import apply_rule, pivot, match_spider_parallel, spider
9
+ from .circuit import Circuit
10
+ from .circuit.gates import Gate, ParityPhase, CNOT, HAD, ZPhase, CZ, InitAncilla
11
+ from .graph.base import BaseGraph, VT, ET
12
+
13
+ from typing import List, Optional, Tuple, Dict, Set, Union
14
+
15
+ from .extract import bi_adj, connectivity_from_biadj, max_overlap
16
+ # , greedy_reduction, find_minimal_sums, xor_rows, column_optimal_swap
17
+
18
+
19
+ # permute the rows of "m" such that as many rows as possible have
20
+ # 1s in position given by "pos"
21
+ # TODO: this can be done in poly-time with graph matching algo
22
+ # (e.g. Hopcroft-Karp)
23
+ def ones_in_pos(m, pos):
24
+ winner = -1
25
+
26
+ perm = None
27
+ for p in itertools.permutations(range(m.rows())):
28
+ score = sum(m[p[i],pos[i]] for i in range(m.rows()))
29
+ if score > winner:
30
+ winner = score
31
+ perm = p
32
+ if score == m.rows():
33
+ break
34
+ m1 = m.copy()
35
+
36
+ if not perm is None:
37
+ for i in range(m.rows()):
38
+ m[perm[i],:] = m1[i,:]
39
+
40
+ # produce a parity matrix that extracts all of the extractable
41
+ # vertices in convenient places and ignores the rest
42
+ def compute_row_ops(m):
43
+ ops = Mat2.id(m.rows())
44
+ m1 = m.copy()
45
+ m1.gauss(full_reduce=True,x=ops)
46
+ #extr_rows = [r for r,row in enumerate(m1.data) if sum(row)==1]
47
+
48
+ # keep only the rows corresponding to extractable verts
49
+ #ops_rows = []
50
+ ops_rows = [ops.data[r]
51
+ for r,row in enumerate(m1.data)
52
+ if sum(row)==1]
53
+ #ops_rows.sort(key=lambda row: sum(row), reverse=False)
54
+ ops_rows.sort(reverse=True)
55
+ #ops_rows = ops_rows[0:2]
56
+ ops = Mat2(ops_rows)
57
+
58
+
59
+ # for r,row in enumerate(m1.data):
60
+ # if sum(row)==1:
61
+ # ops_rows.append(ops.data[r])
62
+ # ops = Mat2(ops_rows)
63
+ # ops = Mat2([ops.data[r]
64
+ # for r,row in enumerate(m1.data)
65
+ # if sum(row)==1])
66
+ # list.sort(ops.data, reverse=True)
67
+
68
+ # find pivot columns
69
+ pivot_cols = []
70
+ ops.copy().gauss(pivot_cols=pivot_cols)
71
+ #ones_in_pos(ops,pivot_cols)
72
+
73
+ # augment ops by adding unit vectors for all non-pivot columns
74
+ ops2 = Mat2.id(m.rows())
75
+ for r in range(ops.rows()):
76
+ ops2[pivot_cols[r],:] = ops[r,:]
77
+ return ops2
78
+
79
+
80
+ def alt_extract_circuit(
81
+ g:BaseGraph[VT,ET],
82
+ optimize_czs:bool=True,
83
+ optimize_cnots:int=2,
84
+ quiet:bool=True
85
+ ) -> Circuit:
86
+ """
87
+ Experiments with the extractor. (not ready for primetime :)
88
+
89
+
90
+ Args:
91
+ g: The ZX-diagram graph to be extracted into a Circuit.
92
+ optimize_czs: Whether to try to optimize the CZ-subcircuits by exploiting overlap between the CZ gates
93
+ optimize_cnots: (0,1,2,3) Level of CNOT optimization to apply.
94
+ quiet: Whether to print detailed output of the extraction process.
95
+ """
96
+ qs = g.qubits() # We are assuming that these are objects that update...
97
+ rs = g.rows() # ...to reflect changes to the graph, so that when...
98
+ # ty = g.types() # ... g.set_row/g.set_qubit is called, these things update directly to reflect that
99
+ phases = g.phases()
100
+ c = Circuit(g.qubit_count())
101
+
102
+ gadgets = {}
103
+ inputs = g.inputs()
104
+ outputs = g.outputs()
105
+ for v in g.vertices():
106
+ if g.vertex_degree(v) == 1 and v not in inputs and v not in outputs:
107
+ n = list(g.neighbors(v))[0]
108
+ gadgets[n] = v
109
+
110
+ qubit_map: Dict[VT,int] = dict()
111
+ frontier = []
112
+ for i,o in enumerate(outputs):
113
+ v = list(g.neighbors(o))[0]
114
+ if v in inputs: continue
115
+ frontier.append(v)
116
+ qubit_map[v] = i
117
+
118
+ czs_saved = 0
119
+ q: Union[float,int]
120
+
121
+ while True:
122
+ # preprocessing
123
+
124
+ for v in frontier: # First removing single qubit gates
125
+ q = qubit_map[v]
126
+ b = [w for w in g.neighbors(v) if w in outputs][0]
127
+ e = g.edge(v,b)
128
+ if g.edge_type(e) == 2: # Hadamard edge
129
+ c.add_gate("HAD",q)
130
+ g.set_edge_type(e, EdgeType.SIMPLE)
131
+ if phases[v]:
132
+ c.add_gate("ZPhase", q, phases[v])
133
+ g.set_phase(v,0)
134
+
135
+ # And now on to CZ gates
136
+ cz_mat = Mat2([[0 for i in range(g.qubit_count())] for j in range(g.qubit_count())])
137
+ for v in frontier:
138
+ for w in list(g.neighbors(v)):
139
+ if w in frontier:
140
+ cz_mat.data[qubit_map[v]][qubit_map[w]] = 1
141
+ cz_mat.data[qubit_map[w]][qubit_map[v]] = 1
142
+ g.remove_edge(g.edge(v,w))
143
+
144
+ if optimize_czs:
145
+ overlap_data = max_overlap(cz_mat)
146
+ while len(overlap_data[1]) > 2: #there are enough common qubits to be worth optimizing
147
+ i,j = overlap_data[0][0], overlap_data[0][1]
148
+ czs_saved += len(overlap_data[1])-2
149
+ c.add_gate("CNOT",i,j)
150
+ for qb in overlap_data[1]:
151
+ c.add_gate("CZ",j,qb)
152
+ cz_mat.data[i][qb]=0
153
+ cz_mat.data[j][qb]=0
154
+ cz_mat.data[qb][i]=0
155
+ cz_mat.data[qb][j]=0
156
+ c.add_gate("CNOT",i,j)
157
+ overlap_data = max_overlap(cz_mat)
158
+
159
+ for i in range(g.qubit_count()):
160
+ for j in range(i+1,g.qubit_count()):
161
+ if cz_mat.data[i][j]==1:
162
+ c.add_gate("CZ",i,j)
163
+
164
+ # Now we can proceed with the actual extraction
165
+ # First make sure that frontier is connected in correct way to inputs
166
+ neighbor_set = set()
167
+ for v in frontier.copy():
168
+ d = [w for w in g.neighbors(v) if w not in outputs]
169
+ if any(w in inputs for w in d): #frontier vertex v is connected to an input
170
+ if len(d) == 1: # Only connected to input, remove from frontier
171
+ frontier.remove(v)
172
+ continue
173
+ # We disconnect v from the input b via a new spider
174
+ b = [w for w in d if w in inputs][0]
175
+ q = qs[b]
176
+ r = rs[b]
177
+ w = g.add_vertex(VertexType.Z, q, r+1)
178
+ e = g.edge(v,b)
179
+ et = g.edge_type(e)
180
+ g.remove_edge(e)
181
+ g.add_edge((v, w), EdgeType.HADAMARD)
182
+ g.add_edge((w,b),toggle_edge(et))
183
+ d.remove(b)
184
+ d.append(w)
185
+ neighbor_set.update(d)
186
+
187
+ if not frontier: break # No more vertices to be processed. We are done.
188
+
189
+ # First we check if there is a phase gadget in the way
190
+ removed_gadget = False
191
+ for w in neighbor_set:
192
+ if w not in gadgets: continue
193
+ for v in g.neighbors(w):
194
+ if v in frontier:
195
+ apply_rule(g,pivot,[(w,v,[],[o for o in g.neighbors(v) if o in g.outputs])]) # type: ignore
196
+ frontier.remove(v)
197
+ del gadgets[w]
198
+ frontier.append(w)
199
+ qubit_map[w] = qubit_map[v]
200
+ removed_gadget = True
201
+ break
202
+ if removed_gadget: # There was indeed a gadget in the way. Go back to the top
203
+ continue
204
+
205
+ neighbors = list(neighbor_set)
206
+ m = bi_adj(g,neighbors,frontier)
207
+ ops = compute_row_ops(m)
208
+ m1 = ops * m
209
+
210
+ cnots = Circuit(g.qubit_count())
211
+ blocksize = math.ceil(math.log(g.qubit_count(),2)) * 2
212
+ winner = -1
213
+ for bs in range(1,blocksize):
214
+ cnots1 = Circuit(g.qubit_count())
215
+ ops.copy().gauss(full_reduce=True,
216
+ y=cnots1, blocksize=bs)
217
+ if winner == -1 or len(cnots1.gates) < winner:
218
+ cnots = cnots1
219
+
220
+ #return m, ops
221
+
222
+ connectivity_from_biadj(g,m1,neighbors,frontier)
223
+
224
+ good_verts = dict()
225
+ for i, row in enumerate(m1.data):
226
+ if sum(row) == 1:
227
+ v = frontier[i]
228
+ w = neighbors[[j for j in range(len(row)) if row[j]][0]]
229
+ good_verts[v] = w
230
+
231
+ # if not quiet: print("good_verts:", good_verts)
232
+ if not good_verts: #raise Exception("No extractable vertex found. Something went wrong")
233
+ print("No extractable vertex found. Something went wrong")
234
+ break
235
+ hads = []
236
+ for v,w in good_verts.items(): # Update frontier vertices
237
+ hads.append(qubit_map[v])
238
+ #c.add_gate("HAD",qubit_map[v])
239
+ qubit_map[w] = qubit_map[v]
240
+ b = [o for o in g.neighbors(v) if o in outputs][0]
241
+ g.remove_vertex(v)
242
+ g.add_edge((w,b))
243
+ frontier.remove(v)
244
+ frontier.append(w)
245
+ if not quiet: print("Vertices extracted:", len(good_verts))
246
+ #for cnot in cnots: c.add_gate(cnot)
247
+ c.add_circuit(cnots)
248
+ for h in hads: c.add_gate("HAD",h)
249
+ return c
250
+ # TODO: finalise