qiskit 1.1.2__cp38-abi3-win32.whl → 1.2.0rc1__cp38-abi3-win32.whl

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 (341) hide show
  1. qiskit/VERSION.txt +1 -1
  2. qiskit/__init__.py +27 -24
  3. qiskit/_accelerate.pyd +0 -0
  4. qiskit/_numpy_compat.py +1 -1
  5. qiskit/assembler/assemble_circuits.py +107 -64
  6. qiskit/assembler/assemble_schedules.py +5 -12
  7. qiskit/assembler/disassemble.py +10 -1
  8. qiskit/circuit/__init__.py +1 -1
  9. qiskit/circuit/_classical_resource_map.py +5 -5
  10. qiskit/circuit/_utils.py +0 -13
  11. qiskit/circuit/add_control.py +1 -1
  12. qiskit/circuit/annotated_operation.py +23 -1
  13. qiskit/circuit/classical/expr/expr.py +4 -4
  14. qiskit/circuit/classical/expr/visitors.py +1 -1
  15. qiskit/circuit/classical/types/__init__.py +1 -1
  16. qiskit/circuit/classical/types/types.py +2 -2
  17. qiskit/circuit/classicalfunction/boolean_expression.py +1 -1
  18. qiskit/circuit/classicalfunction/classical_function_visitor.py +5 -5
  19. qiskit/circuit/classicalfunction/utils.py +1 -1
  20. qiskit/circuit/classicalregister.py +1 -1
  21. qiskit/circuit/commutation_checker.py +83 -35
  22. qiskit/circuit/controlflow/_builder_utils.py +1 -1
  23. qiskit/circuit/controlflow/builder.py +10 -6
  24. qiskit/circuit/controlflow/if_else.py +2 -2
  25. qiskit/circuit/controlflow/switch_case.py +1 -1
  26. qiskit/circuit/delay.py +1 -1
  27. qiskit/circuit/duration.py +2 -2
  28. qiskit/circuit/equivalence.py +5 -7
  29. qiskit/circuit/gate.py +11 -8
  30. qiskit/circuit/instruction.py +31 -13
  31. qiskit/circuit/instructionset.py +2 -5
  32. qiskit/circuit/library/__init__.py +2 -1
  33. qiskit/circuit/library/arithmetic/linear_amplitude_function.py +1 -1
  34. qiskit/circuit/library/arithmetic/linear_pauli_rotations.py +1 -1
  35. qiskit/circuit/library/arithmetic/piecewise_chebyshev.py +1 -1
  36. qiskit/circuit/library/arithmetic/piecewise_linear_pauli_rotations.py +1 -1
  37. qiskit/circuit/library/arithmetic/piecewise_polynomial_pauli_rotations.py +3 -3
  38. qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py +1 -1
  39. qiskit/circuit/library/basis_change/__init__.py +1 -1
  40. qiskit/circuit/library/basis_change/qft.py +40 -6
  41. qiskit/circuit/library/blueprintcircuit.py +3 -5
  42. qiskit/circuit/library/data_preparation/__init__.py +9 -2
  43. qiskit/circuit/library/data_preparation/initializer.py +8 -0
  44. qiskit/circuit/library/data_preparation/state_preparation.py +98 -178
  45. qiskit/circuit/library/generalized_gates/isometry.py +8 -8
  46. qiskit/circuit/library/generalized_gates/linear_function.py +3 -2
  47. qiskit/circuit/library/generalized_gates/mcg_up_to_diagonal.py +4 -4
  48. qiskit/circuit/library/generalized_gates/permutation.py +8 -9
  49. qiskit/circuit/library/generalized_gates/uc.py +3 -3
  50. qiskit/circuit/library/generalized_gates/uc_pauli_rot.py +2 -2
  51. qiskit/circuit/library/generalized_gates/unitary.py +13 -11
  52. qiskit/circuit/library/graph_state.py +1 -1
  53. qiskit/circuit/library/hamiltonian_gate.py +1 -2
  54. qiskit/circuit/library/hidden_linear_function.py +1 -1
  55. qiskit/circuit/library/n_local/evolved_operator_ansatz.py +3 -2
  56. qiskit/circuit/library/n_local/n_local.py +4 -5
  57. qiskit/circuit/library/n_local/pauli_two_design.py +1 -1
  58. qiskit/circuit/library/n_local/qaoa_ansatz.py +6 -8
  59. qiskit/circuit/library/n_local/two_local.py +1 -1
  60. qiskit/circuit/library/overlap.py +11 -5
  61. qiskit/circuit/library/pauli_evolution.py +7 -3
  62. qiskit/circuit/library/standard_gates/dcx.py +3 -0
  63. qiskit/circuit/library/standard_gates/ecr.py +3 -0
  64. qiskit/circuit/library/standard_gates/global_phase.py +3 -0
  65. qiskit/circuit/library/standard_gates/h.py +13 -5
  66. qiskit/circuit/library/standard_gates/i.py +3 -0
  67. qiskit/circuit/library/standard_gates/iswap.py +3 -0
  68. qiskit/circuit/library/standard_gates/multi_control_rotation_gates.py +19 -10
  69. qiskit/circuit/library/standard_gates/p.py +14 -9
  70. qiskit/circuit/library/standard_gates/r.py +3 -0
  71. qiskit/circuit/library/standard_gates/rx.py +21 -6
  72. qiskit/circuit/library/standard_gates/rxx.py +40 -1
  73. qiskit/circuit/library/standard_gates/ry.py +21 -6
  74. qiskit/circuit/library/standard_gates/ryy.py +40 -1
  75. qiskit/circuit/library/standard_gates/rz.py +22 -6
  76. qiskit/circuit/library/standard_gates/rzx.py +40 -1
  77. qiskit/circuit/library/standard_gates/rzz.py +41 -2
  78. qiskit/circuit/library/standard_gates/s.py +77 -0
  79. qiskit/circuit/library/standard_gates/swap.py +12 -5
  80. qiskit/circuit/library/standard_gates/sx.py +14 -5
  81. qiskit/circuit/library/standard_gates/t.py +5 -0
  82. qiskit/circuit/library/standard_gates/u.py +22 -7
  83. qiskit/circuit/library/standard_gates/u1.py +8 -3
  84. qiskit/circuit/library/standard_gates/u2.py +3 -0
  85. qiskit/circuit/library/standard_gates/u3.py +22 -7
  86. qiskit/circuit/library/standard_gates/x.py +156 -92
  87. qiskit/circuit/library/standard_gates/xx_minus_yy.py +40 -1
  88. qiskit/circuit/library/standard_gates/xx_plus_yy.py +52 -11
  89. qiskit/circuit/library/standard_gates/y.py +6 -1
  90. qiskit/circuit/library/standard_gates/z.py +8 -1
  91. qiskit/circuit/operation.py +1 -1
  92. qiskit/circuit/parameter.py +9 -10
  93. qiskit/circuit/parameterexpression.py +16 -13
  94. qiskit/circuit/parametertable.py +1 -190
  95. qiskit/circuit/parametervector.py +1 -1
  96. qiskit/circuit/quantumcircuit.py +392 -384
  97. qiskit/circuit/quantumcircuitdata.py +3 -5
  98. qiskit/circuit/quantumregister.py +1 -1
  99. qiskit/circuit/random/__init__.py +1 -1
  100. qiskit/circuit/random/utils.py +175 -26
  101. qiskit/circuit/register.py +5 -7
  102. qiskit/circuit/singleton.py +3 -3
  103. qiskit/circuit/tools/pi_check.py +4 -4
  104. qiskit/compiler/assembler.py +95 -24
  105. qiskit/compiler/scheduler.py +2 -2
  106. qiskit/compiler/transpiler.py +41 -127
  107. qiskit/converters/circuit_to_dag.py +4 -6
  108. qiskit/converters/circuit_to_gate.py +4 -8
  109. qiskit/converters/circuit_to_instruction.py +5 -17
  110. qiskit/converters/dag_to_circuit.py +2 -6
  111. qiskit/dagcircuit/collect_blocks.py +2 -2
  112. qiskit/dagcircuit/dagcircuit.py +190 -187
  113. qiskit/dagcircuit/dagdependency.py +4 -4
  114. qiskit/dagcircuit/dagdependency_v2.py +4 -4
  115. qiskit/dagcircuit/dagdepnode.py +1 -1
  116. qiskit/dagcircuit/dagnode.py +66 -157
  117. qiskit/passmanager/flow_controllers.py +1 -1
  118. qiskit/passmanager/passmanager.py +3 -3
  119. qiskit/primitives/__init__.py +1 -5
  120. qiskit/primitives/backend_estimator.py +25 -15
  121. qiskit/primitives/backend_estimator_v2.py +31 -7
  122. qiskit/primitives/backend_sampler.py +21 -12
  123. qiskit/primitives/backend_sampler_v2.py +12 -3
  124. qiskit/primitives/base/base_estimator.py +31 -4
  125. qiskit/primitives/base/base_primitive.py +2 -2
  126. qiskit/primitives/base/base_result.py +2 -2
  127. qiskit/primitives/base/base_sampler.py +26 -2
  128. qiskit/primitives/base/estimator_result.py +2 -2
  129. qiskit/primitives/base/sampler_result.py +2 -2
  130. qiskit/primitives/containers/__init__.py +0 -1
  131. qiskit/primitives/containers/bindings_array.py +2 -2
  132. qiskit/primitives/containers/bit_array.py +108 -10
  133. qiskit/primitives/containers/shape.py +3 -3
  134. qiskit/primitives/estimator.py +9 -2
  135. qiskit/primitives/primitive_job.py +1 -1
  136. qiskit/primitives/sampler.py +10 -3
  137. qiskit/primitives/statevector_estimator.py +5 -3
  138. qiskit/primitives/statevector_sampler.py +11 -5
  139. qiskit/primitives/utils.py +16 -0
  140. qiskit/providers/backend.py +15 -6
  141. qiskit/providers/backend_compat.py +7 -4
  142. qiskit/providers/basic_provider/basic_provider_tools.py +1 -1
  143. qiskit/providers/basic_provider/basic_simulator.py +32 -24
  144. qiskit/providers/fake_provider/fake_backend.py +10 -3
  145. qiskit/providers/fake_provider/fake_openpulse_2q.py +154 -146
  146. qiskit/providers/fake_provider/fake_openpulse_3q.py +226 -217
  147. qiskit/providers/fake_provider/fake_qasm_backend.py +5 -1
  148. qiskit/providers/fake_provider/generic_backend_v2.py +80 -50
  149. qiskit/providers/models/__init__.py +11 -0
  150. qiskit/providers/models/backendconfiguration.py +50 -4
  151. qiskit/providers/models/backendproperties.py +13 -2
  152. qiskit/providers/models/pulsedefaults.py +10 -11
  153. qiskit/providers/options.py +13 -13
  154. qiskit/providers/providerutils.py +3 -1
  155. qiskit/pulse/configuration.py +8 -12
  156. qiskit/pulse/instruction_schedule_map.py +3 -5
  157. qiskit/pulse/instructions/acquire.py +7 -8
  158. qiskit/pulse/instructions/instruction.py +2 -3
  159. qiskit/pulse/library/samplers/decorators.py +5 -9
  160. qiskit/pulse/library/symbolic_pulses.py +4 -7
  161. qiskit/pulse/library/waveform.py +2 -5
  162. qiskit/pulse/macros.py +11 -6
  163. qiskit/pulse/parser.py +8 -10
  164. qiskit/pulse/schedule.py +9 -17
  165. qiskit/pulse/transforms/alignments.py +1 -3
  166. qiskit/pulse/utils.py +1 -2
  167. qiskit/qasm/libs/stdgates.inc +35 -28
  168. qiskit/qasm2/__init__.py +7 -7
  169. qiskit/qasm2/export.py +5 -9
  170. qiskit/qasm2/parse.py +1 -1
  171. qiskit/qasm3/ast.py +9 -25
  172. qiskit/qasm3/exporter.py +578 -481
  173. qiskit/qasm3/printer.py +7 -16
  174. qiskit/qobj/common.py +10 -0
  175. qiskit/qobj/converters/lo_config.py +9 -0
  176. qiskit/qobj/converters/pulse_instruction.py +13 -6
  177. qiskit/qobj/pulse_qobj.py +69 -15
  178. qiskit/qobj/qasm_qobj.py +72 -20
  179. qiskit/qobj/utils.py +9 -0
  180. qiskit/qpy/binary_io/circuits.py +8 -5
  181. qiskit/qpy/binary_io/schedules.py +1 -1
  182. qiskit/qpy/binary_io/value.py +3 -3
  183. qiskit/qpy/interface.py +3 -2
  184. qiskit/qpy/type_keys.py +2 -2
  185. qiskit/quantum_info/operators/channel/quantum_channel.py +3 -6
  186. qiskit/quantum_info/operators/channel/superop.py +2 -2
  187. qiskit/quantum_info/operators/channel/transformations.py +1 -1
  188. qiskit/quantum_info/operators/dihedral/dihedral.py +3 -4
  189. qiskit/quantum_info/operators/dihedral/dihedral_circuits.py +1 -3
  190. qiskit/quantum_info/operators/dihedral/random.py +6 -3
  191. qiskit/quantum_info/operators/measures.py +2 -2
  192. qiskit/quantum_info/operators/op_shape.py +12 -20
  193. qiskit/quantum_info/operators/operator.py +14 -21
  194. qiskit/quantum_info/operators/predicates.py +1 -0
  195. qiskit/quantum_info/operators/symplectic/base_pauli.py +7 -11
  196. qiskit/quantum_info/operators/symplectic/clifford.py +1 -1
  197. qiskit/quantum_info/operators/symplectic/pauli.py +3 -3
  198. qiskit/quantum_info/operators/symplectic/pauli_list.py +9 -10
  199. qiskit/quantum_info/operators/symplectic/random.py +1 -1
  200. qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +14 -16
  201. qiskit/quantum_info/quaternion.py +1 -1
  202. qiskit/quantum_info/states/densitymatrix.py +5 -8
  203. qiskit/quantum_info/states/stabilizerstate.py +128 -37
  204. qiskit/quantum_info/states/statevector.py +4 -8
  205. qiskit/result/counts.py +2 -2
  206. qiskit/result/mitigation/correlated_readout_mitigator.py +2 -2
  207. qiskit/result/mitigation/local_readout_mitigator.py +2 -2
  208. qiskit/result/mitigation/utils.py +1 -3
  209. qiskit/result/models.py +17 -16
  210. qiskit/result/result.py +15 -20
  211. qiskit/scheduler/lowering.py +2 -2
  212. qiskit/synthesis/__init__.py +2 -1
  213. qiskit/synthesis/clifford/__init__.py +1 -1
  214. qiskit/synthesis/clifford/clifford_decompose_ag.py +2 -2
  215. qiskit/synthesis/clifford/clifford_decompose_bm.py +10 -240
  216. qiskit/synthesis/clifford/clifford_decompose_greedy.py +9 -303
  217. qiskit/synthesis/clifford/clifford_decompose_layers.py +25 -23
  218. qiskit/synthesis/cnotdihedral/cnotdihedral_decompose_full.py +1 -1
  219. qiskit/synthesis/cnotdihedral/cnotdihedral_decompose_general.py +1 -1
  220. qiskit/synthesis/discrete_basis/generate_basis_approximations.py +1 -1
  221. qiskit/synthesis/discrete_basis/solovay_kitaev.py +2 -2
  222. qiskit/synthesis/evolution/evolution_synthesis.py +4 -2
  223. qiskit/synthesis/evolution/lie_trotter.py +46 -19
  224. qiskit/synthesis/evolution/product_formula.py +111 -55
  225. qiskit/synthesis/evolution/qdrift.py +40 -10
  226. qiskit/synthesis/evolution/suzuki_trotter.py +43 -33
  227. qiskit/synthesis/linear/__init__.py +1 -0
  228. qiskit/synthesis/linear/cnot_synth.py +22 -96
  229. qiskit/synthesis/linear/linear_depth_lnn.py +8 -8
  230. qiskit/synthesis/linear/linear_matrix_utils.py +13 -161
  231. qiskit/synthesis/linear_phase/cnot_phase_synth.py +1 -1
  232. qiskit/synthesis/linear_phase/cx_cz_depth_lnn.py +3 -3
  233. qiskit/synthesis/linear_phase/cz_depth_lnn.py +1 -1
  234. qiskit/synthesis/one_qubit/one_qubit_decompose.py +29 -29
  235. qiskit/synthesis/permutation/permutation_full.py +5 -29
  236. qiskit/synthesis/permutation/permutation_lnn.py +2 -24
  237. qiskit/synthesis/permutation/permutation_utils.py +2 -59
  238. qiskit/synthesis/qft/__init__.py +1 -0
  239. qiskit/synthesis/qft/qft_decompose_full.py +79 -0
  240. qiskit/synthesis/qft/qft_decompose_lnn.py +17 -9
  241. qiskit/synthesis/stabilizer/stabilizer_circuit.py +6 -6
  242. qiskit/synthesis/stabilizer/stabilizer_decompose.py +2 -2
  243. qiskit/synthesis/two_qubit/local_invariance.py +8 -38
  244. qiskit/synthesis/two_qubit/two_qubit_decompose.py +48 -129
  245. qiskit/synthesis/unitary/aqc/cnot_structures.py +1 -1
  246. qiskit/synthesis/unitary/qsd.py +5 -3
  247. qiskit/transpiler/__init__.py +1 -0
  248. qiskit/transpiler/basepasses.py +1 -1
  249. qiskit/transpiler/coupling.py +3 -3
  250. qiskit/transpiler/instruction_durations.py +1 -2
  251. qiskit/transpiler/layout.py +3 -3
  252. qiskit/transpiler/passes/__init__.py +2 -0
  253. qiskit/transpiler/passes/basis/basis_translator.py +82 -63
  254. qiskit/transpiler/passes/basis/translate_parameterized.py +3 -5
  255. qiskit/transpiler/passes/basis/unroll_3q_or_more.py +1 -1
  256. qiskit/transpiler/passes/basis/unroll_custom_definitions.py +10 -10
  257. qiskit/transpiler/passes/calibration/rx_builder.py +3 -3
  258. qiskit/transpiler/passes/calibration/rzx_builder.py +3 -3
  259. qiskit/transpiler/passes/layout/apply_layout.py +13 -3
  260. qiskit/transpiler/passes/layout/sabre_layout.py +10 -8
  261. qiskit/transpiler/passes/layout/sabre_pre_layout.py +4 -1
  262. qiskit/transpiler/passes/layout/set_layout.py +2 -2
  263. qiskit/transpiler/passes/layout/vf2_layout.py +1 -1
  264. qiskit/transpiler/passes/layout/vf2_utils.py +3 -3
  265. qiskit/transpiler/passes/optimization/__init__.py +1 -0
  266. qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py +2 -2
  267. qiskit/transpiler/passes/optimization/commutation_analysis.py +7 -10
  268. qiskit/transpiler/passes/optimization/commutative_cancellation.py +35 -19
  269. qiskit/transpiler/passes/optimization/consolidate_blocks.py +11 -8
  270. qiskit/transpiler/passes/optimization/inverse_cancellation.py +6 -6
  271. qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py +64 -41
  272. qiskit/transpiler/passes/optimization/optimize_1q_gates.py +1 -1
  273. qiskit/transpiler/passes/optimization/split_2q_unitaries.py +83 -0
  274. qiskit/transpiler/passes/optimization/template_matching/backward_match.py +1 -1
  275. qiskit/transpiler/passes/optimization/template_matching/forward_match.py +2 -2
  276. qiskit/transpiler/passes/optimization/template_matching/template_substitution.py +1 -1
  277. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_gate_router.py +3 -2
  278. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/swap_strategy.py +1 -1
  279. qiskit/transpiler/passes/routing/layout_transformation.py +2 -1
  280. qiskit/transpiler/passes/routing/sabre_swap.py +35 -26
  281. qiskit/transpiler/passes/routing/star_prerouting.py +80 -105
  282. qiskit/transpiler/passes/routing/stochastic_swap.py +1 -3
  283. qiskit/transpiler/passes/scheduling/alap.py +1 -2
  284. qiskit/transpiler/passes/scheduling/alignments/__init__.py +2 -2
  285. qiskit/transpiler/passes/scheduling/alignments/check_durations.py +1 -1
  286. qiskit/transpiler/passes/scheduling/alignments/pulse_gate_validation.py +1 -1
  287. qiskit/transpiler/passes/scheduling/alignments/reschedule.py +1 -1
  288. qiskit/transpiler/passes/scheduling/asap.py +1 -2
  289. qiskit/transpiler/passes/scheduling/base_scheduler.py +5 -5
  290. qiskit/transpiler/passes/scheduling/dynamical_decoupling.py +3 -3
  291. qiskit/transpiler/passes/scheduling/padding/base_padding.py +1 -1
  292. qiskit/transpiler/passes/scheduling/padding/dynamical_decoupling.py +11 -11
  293. qiskit/transpiler/passes/scheduling/scheduling/base_scheduler.py +7 -6
  294. qiskit/transpiler/passes/scheduling/time_unit_conversion.py +4 -3
  295. qiskit/transpiler/passes/synthesis/high_level_synthesis.py +211 -36
  296. qiskit/transpiler/passes/synthesis/plugin.py +2 -2
  297. qiskit/transpiler/passes/synthesis/unitary_synthesis.py +80 -40
  298. qiskit/transpiler/passes/utils/__init__.py +0 -1
  299. qiskit/transpiler/passes/utils/check_gate_direction.py +4 -4
  300. qiskit/transpiler/passes/utils/check_map.py +3 -6
  301. qiskit/transpiler/passes/utils/convert_conditions_to_if_ops.py +3 -4
  302. qiskit/transpiler/passes/utils/error.py +2 -2
  303. qiskit/transpiler/passes/utils/fixed_point.py +3 -3
  304. qiskit/transpiler/passes/utils/gate_direction.py +1 -1
  305. qiskit/transpiler/passes/utils/gates_basis.py +1 -2
  306. qiskit/transpiler/passmanager.py +7 -6
  307. qiskit/transpiler/preset_passmanagers/__init__.py +4 -228
  308. qiskit/transpiler/preset_passmanagers/builtin_plugins.py +117 -18
  309. qiskit/transpiler/preset_passmanagers/common.py +3 -6
  310. qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py +518 -0
  311. qiskit/transpiler/preset_passmanagers/level0.py +1 -1
  312. qiskit/transpiler/target.py +27 -8
  313. qiskit/user_config.py +29 -6
  314. qiskit/utils/classtools.py +3 -3
  315. qiskit/utils/deprecation.py +3 -2
  316. qiskit/utils/lazy_tester.py +2 -2
  317. qiskit/utils/optionals.py +8 -8
  318. qiskit/visualization/bloch.py +19 -67
  319. qiskit/visualization/circuit/_utils.py +34 -10
  320. qiskit/visualization/circuit/circuit_visualization.py +23 -16
  321. qiskit/visualization/circuit/latex.py +29 -27
  322. qiskit/visualization/circuit/matplotlib.py +4 -2
  323. qiskit/visualization/circuit/qcstyle.py +2 -2
  324. qiskit/visualization/circuit/text.py +9 -15
  325. qiskit/visualization/dag_visualization.py +5 -12
  326. qiskit/visualization/pulse_v2/core.py +1 -1
  327. qiskit/visualization/pulse_v2/events.py +1 -1
  328. qiskit/visualization/pulse_v2/generators/frame.py +3 -4
  329. qiskit/visualization/pulse_v2/generators/waveform.py +5 -9
  330. qiskit/visualization/pulse_v2/layouts.py +1 -5
  331. qiskit/visualization/pulse_v2/plotters/matplotlib.py +1 -2
  332. qiskit/visualization/state_visualization.py +5 -6
  333. qiskit/visualization/timeline/plotters/matplotlib.py +1 -2
  334. qiskit/visualization/transition_visualization.py +7 -2
  335. {qiskit-1.1.2.dist-info → qiskit-1.2.0rc1.dist-info}/METADATA +26 -26
  336. {qiskit-1.1.2.dist-info → qiskit-1.2.0rc1.dist-info}/RECORD +340 -338
  337. {qiskit-1.1.2.dist-info → qiskit-1.2.0rc1.dist-info}/WHEEL +1 -1
  338. {qiskit-1.1.2.dist-info → qiskit-1.2.0rc1.dist-info}/entry_points.txt +3 -0
  339. qiskit/transpiler/passes/utils/block_to_matrix.py +0 -47
  340. {qiskit-1.1.2.dist-info → qiskit-1.2.0rc1.dist-info}/LICENSE.txt +0 -0
  341. {qiskit-1.1.2.dist-info → qiskit-1.2.0rc1.dist-info}/top_level.txt +0 -0
qiskit/qasm3/exporter.py CHANGED
@@ -12,20 +12,26 @@
12
12
 
13
13
  """QASM3 Exporter"""
14
14
 
15
+ from __future__ import annotations
16
+
15
17
  import collections
16
- import re
18
+ import contextlib
19
+ import dataclasses
17
20
  import io
18
21
  import itertools
22
+ import math
19
23
  import numbers
20
- from os.path import dirname, join, abspath
24
+ import re
21
25
  from typing import Iterable, List, Sequence, Union
22
26
 
27
+ from qiskit._accelerate.circuit import StandardGate
23
28
  from qiskit.circuit import (
29
+ library,
24
30
  Barrier,
25
31
  CircuitInstruction,
26
32
  Clbit,
33
+ ControlledGate,
27
34
  Gate,
28
- Instruction,
29
35
  Measure,
30
36
  Parameter,
31
37
  ParameterExpression,
@@ -47,7 +53,6 @@ from qiskit.circuit.controlflow import (
47
53
  ContinueLoopOp,
48
54
  CASE_DEFAULT,
49
55
  )
50
- from qiskit.circuit.library import standard_gates
51
56
  from qiskit.circuit.register import Register
52
57
  from qiskit.circuit.tools import pi_check
53
58
 
@@ -115,13 +120,9 @@ _RESERVED_KEYWORDS = frozenset(
115
120
  # This probably isn't precisely the same as the OQ3 spec, but we'd need an extra dependency to fully
116
121
  # handle all Unicode character classes, and this should be close enough for users who aren't
117
122
  # actively _trying_ to break us (fingers crossed).
118
- _VALID_IDENTIFIER = re.compile(r"[\w][\w\d]*", flags=re.U)
119
-
120
-
121
- def _escape_invalid_identifier(name: str) -> str:
122
- if name in _RESERVED_KEYWORDS or not _VALID_IDENTIFIER.fullmatch(name):
123
- name = "_" + re.sub(r"[^\w\d]", "_", name)
124
- return name
123
+ _VALID_DECLARABLE_IDENTIFIER = re.compile(r"([\w][\w\d]*)", flags=re.U)
124
+ _VALID_HARDWARE_QUBIT = re.compile(r"\$[\d]+", flags=re.U)
125
+ _BAD_IDENTIFIER_CHARACTERS = re.compile(r"[^\w\d]", flags=re.U)
125
126
 
126
127
 
127
128
  class Exporter:
@@ -196,134 +197,325 @@ class Exporter:
196
197
  )
197
198
 
198
199
 
199
- class GlobalNamespace:
200
- """Global namespace dict-like."""
201
-
202
- BASIS_GATE = object()
203
-
204
- qiskit_gates = {
205
- "p": standard_gates.PhaseGate,
206
- "x": standard_gates.XGate,
207
- "y": standard_gates.YGate,
208
- "z": standard_gates.ZGate,
209
- "h": standard_gates.HGate,
210
- "s": standard_gates.SGate,
211
- "sdg": standard_gates.SdgGate,
212
- "t": standard_gates.TGate,
213
- "tdg": standard_gates.TdgGate,
214
- "sx": standard_gates.SXGate,
215
- "rx": standard_gates.RXGate,
216
- "ry": standard_gates.RYGate,
217
- "rz": standard_gates.RZGate,
218
- "cx": standard_gates.CXGate,
219
- "cy": standard_gates.CYGate,
220
- "cz": standard_gates.CZGate,
221
- "cp": standard_gates.CPhaseGate,
222
- "crx": standard_gates.CRXGate,
223
- "cry": standard_gates.CRYGate,
224
- "crz": standard_gates.CRZGate,
225
- "ch": standard_gates.CHGate,
226
- "swap": standard_gates.SwapGate,
227
- "ccx": standard_gates.CCXGate,
228
- "cswap": standard_gates.CSwapGate,
229
- "cu": standard_gates.CUGate,
230
- "CX": standard_gates.CXGate,
231
- "phase": standard_gates.PhaseGate,
232
- "cphase": standard_gates.CPhaseGate,
233
- "id": standard_gates.IGate,
234
- "u1": standard_gates.U1Gate,
235
- "u2": standard_gates.U2Gate,
236
- "u3": standard_gates.U3Gate,
237
- }
238
- include_paths = [abspath(join(dirname(__file__), "..", "qasm", "libs"))]
200
+ # Just needs to have enough parameters to support the largest standard (non-controlled) gate in our
201
+ # standard library. We have to use the same `Parameter` instances each time so the equality
202
+ # comparisons will work.
203
+ _FIXED_PARAMETERS = (Parameter("p0"), Parameter("p1"), Parameter("p2"), Parameter("p3"))
204
+
205
+ # Mapping of symbols defined by `stdgates.inc` to their gate definition source.
206
+ _KNOWN_INCLUDES = {
207
+ "stdgates.inc": {
208
+ "p": library.PhaseGate(*_FIXED_PARAMETERS[:1]),
209
+ "x": library.XGate(),
210
+ "y": library.YGate(),
211
+ "z": library.ZGate(),
212
+ "h": library.HGate(),
213
+ "s": library.SGate(),
214
+ "sdg": library.SdgGate(),
215
+ "t": library.TGate(),
216
+ "tdg": library.TdgGate(),
217
+ "sx": library.SXGate(),
218
+ "rx": library.RXGate(*_FIXED_PARAMETERS[:1]),
219
+ "ry": library.RYGate(*_FIXED_PARAMETERS[:1]),
220
+ "rz": library.RZGate(*_FIXED_PARAMETERS[:1]),
221
+ "cx": library.CXGate(),
222
+ "cy": library.CYGate(),
223
+ "cz": library.CZGate(),
224
+ "cp": library.CPhaseGate(*_FIXED_PARAMETERS[:1]),
225
+ "crx": library.CRXGate(*_FIXED_PARAMETERS[:1]),
226
+ "cry": library.CRYGate(*_FIXED_PARAMETERS[:1]),
227
+ "crz": library.CRZGate(*_FIXED_PARAMETERS[:1]),
228
+ "ch": library.CHGate(),
229
+ "swap": library.SwapGate(),
230
+ "ccx": library.CCXGate(),
231
+ "cswap": library.CSwapGate(),
232
+ "cu": library.CUGate(*_FIXED_PARAMETERS[:4]),
233
+ "CX": library.CXGate(),
234
+ "phase": library.PhaseGate(*_FIXED_PARAMETERS[:1]),
235
+ "cphase": library.CPhaseGate(*_FIXED_PARAMETERS[:1]),
236
+ "id": library.IGate(),
237
+ "u1": library.U1Gate(*_FIXED_PARAMETERS[:1]),
238
+ "u2": library.U2Gate(*_FIXED_PARAMETERS[:2]),
239
+ "u3": library.U3Gate(*_FIXED_PARAMETERS[:3]),
240
+ },
241
+ }
242
+
243
+ _BUILTIN_GATES = {
244
+ "U": library.UGate(*_FIXED_PARAMETERS[:3]),
245
+ }
246
+
247
+
248
+ @dataclasses.dataclass
249
+ class GateInfo:
250
+ """Symbol-table information on a gate."""
251
+
252
+ canonical: Gate | None
253
+ """The canonical object for the gate. This is a Qiskit object that is not necessarily equal to
254
+ any usage, but is the canonical form in terms of its parameter usage, such as a standard-library
255
+ gate being defined in terms of the `_FIXED_PARAMETERS` objects. A call-site gate whose
256
+ canonical form equals this can use the corresponding symbol as the callee.
257
+
258
+ This can be ``None`` if the gate was an overridden "basis gate" for this export, so no canonical
259
+ form is known."""
260
+ node: ast.QuantumGateDefinition | None
261
+ """An AST node containing the gate definition. This can be ``None`` if the gate came from an
262
+ included file, or is an overridden "basis gate" of the export."""
263
+
264
+
265
+ class SymbolTable:
266
+ """Track Qiskit objects and the OQ3 identifiers used to refer to them."""
267
+
268
+ def __init__(self):
269
+ self.gates: collections.OrderedDict[str, GateInfo | None] = {}
270
+ """Mapping of the symbol name to the "definition source" of the gate, which provides its
271
+ signature and decomposition. The definition source can be `None` if the user set the gate
272
+ as a custom "basis gate".
273
+
274
+ Gates can only be declared in the global scope, so there is just a single look-up for this.
275
+
276
+ This is insertion ordered, and that can be relied on for iteration later."""
277
+ self.standard_gate_idents: dict[StandardGate, ast.Identifier] = {}
278
+ """Mapping of standard gate enumeration values to the identifier we represent that as."""
279
+ self.user_gate_idents: dict[int, ast.Identifier] = {}
280
+ """Mapping of `id`s of user gates to the identifier we use for it."""
281
+
282
+ self.variables: list[dict[str, object]] = [{}]
283
+ """Stack of mappings of variable names to the Qiskit object that represents them.
284
+
285
+ The zeroth index corresponds to the global scope, the highest index to the current scope."""
286
+ self.objects: list[dict[object, ast.Identifier]] = [{}]
287
+ """Stack of mappings of Qiskit objects to the identifier (or subscripted identifier) that
288
+ refers to them. This is similar to the inverse mapping of ``variables``.
289
+
290
+ The zeroth index corresponds to the global scope, the highest index to the current scope."""
291
+
292
+ # Quick-and-dirty method of getting unique salts for names.
293
+ self._counter = itertools.count()
239
294
 
240
- def __init__(self, includelist, basis_gates=()):
241
- self._data = {gate: self.BASIS_GATE for gate in basis_gates}
242
- self._data["U"] = self.BASIS_GATE
295
+ def push_scope(self):
296
+ """Enter a new variable scope."""
297
+ self.variables.append({})
298
+ self.objects.append({})
299
+
300
+ def pop_scope(self):
301
+ """Exit the current scope, returning to a previous scope."""
302
+ self.objects.pop()
303
+ self.variables.pop()
304
+
305
+ def new_context(self) -> SymbolTable:
306
+ """Create a new context, such as for a gate definition.
307
+
308
+ Contexts share the same set of globally defined gates, but have no access to other variables
309
+ defined in any scope."""
310
+ out = SymbolTable()
311
+ out.gates = self.gates
312
+ out.standard_gate_idents = self.standard_gate_idents
313
+ out.user_gate_idents = self.user_gate_idents
314
+ return out
243
315
 
244
- for includefile in includelist:
245
- if includefile == "stdgates.inc":
246
- self._data.update(self.qiskit_gates)
247
- else:
248
- # TODO What do if an inc file is not standard?
249
- # Should it be parsed?
250
- pass
251
-
252
- def __setitem__(self, name_str, instruction):
253
- self._data[name_str] = instruction.base_class
254
- self._data[id(instruction)] = name_str
255
-
256
- def __getitem__(self, key):
257
- if isinstance(key, Instruction):
258
- try:
259
- # Registered gates.
260
- return self._data[id(key)]
261
- except KeyError:
262
- pass
263
- # Built-in gates.
264
- if key.name not in self._data:
265
- raise KeyError(key)
266
- return key.name
267
- return self._data[key]
268
-
269
- def __iter__(self):
270
- return iter(self._data)
271
-
272
- def __contains__(self, instruction):
273
- if isinstance(instruction, standard_gates.UGate):
274
- return True
275
- if id(instruction) in self._data:
276
- return True
277
- if self._data.get(instruction.name) is self.BASIS_GATE:
278
- return True
279
- if type(instruction) in [Gate, Instruction]: # user-defined instructions/gate
280
- return self._data.get(instruction.name, None) == instruction
281
- type_ = self._data.get(instruction.name)
282
- if isinstance(type_, type) and isinstance(instruction, type_):
283
- return True
284
- return False
316
+ def symbol_defined(self, name: str) -> bool:
317
+ """Whether this identifier has a defined meaning already."""
318
+ return (
319
+ name in _RESERVED_KEYWORDS
320
+ or name in self.gates
321
+ or name in itertools.chain.from_iterable(reversed(self.variables))
322
+ )
285
323
 
286
- def has_symbol(self, name: str) -> bool:
287
- """Whether a symbol's name is present in the table."""
288
- return name in self._data
289
-
290
- def register(self, instruction):
291
- """Register an instruction in the namespace"""
292
- # The second part of the condition is a nasty hack to ensure that gates that come with at
293
- # least one parameter always have their id in the name. This is a workaround a bug, where
294
- # gates with parameters do not contain the information required to build the gate definition
295
- # in symbolic form (unless the parameters are all symbolic). The exporter currently
296
- # (2021-12-01) builds gate declarations with parameters in the signature, but then ignores
297
- # those parameters during the body, and just uses the concrete values from the first
298
- # instance of the gate it sees, such as:
299
- # gate rzx(_gate_p_0) _gate_q_0, _gate_q_1 {
300
- # h _gate_q_1;
301
- # cx _gate_q_0, _gate_q_1;
302
- # rz(0.2) _gate_q_1; // <- note the concrete value.
303
- # cx _gate_q_0, _gate_q_1;
304
- # h _gate_q_1;
305
- # }
306
- # This then means that multiple calls to the same gate with different parameters will be
307
- # incorrect. By forcing all gates to be defined including their id, we generate a QASM3
308
- # program that does what was intended, even though the output QASM3 is silly. See gh-7335.
309
- if instruction.name in self._data or (
310
- isinstance(instruction, Gate)
311
- and not all(isinstance(param, Parameter) for param in instruction.params)
312
- ):
313
- key = f"{instruction.name}_{id(instruction)}"
324
+ def can_shadow_symbol(self, name: str) -> bool:
325
+ """Whether a new definition of this symbol can be made within the OpenQASM 3 shadowing
326
+ rules."""
327
+ return (
328
+ name not in self.variables[-1]
329
+ and name not in self.gates
330
+ and name not in _RESERVED_KEYWORDS
331
+ )
332
+
333
+ def escaped_declarable_name(self, name: str, *, allow_rename: bool, unique: bool = False):
334
+ """Get an identifier based on ``name`` that can be safely shadowed within this scope.
335
+
336
+ If ``unique`` is ``True``, then the name is required to be unique across all live scopes,
337
+ not just able to be redefined."""
338
+ name_allowed = (
339
+ (lambda name: not self.symbol_defined(name)) if unique else self.can_shadow_symbol
340
+ )
341
+ valid_identifier = _VALID_DECLARABLE_IDENTIFIER
342
+ if allow_rename:
343
+ if not valid_identifier.fullmatch(name):
344
+ name = "_" + _BAD_IDENTIFIER_CHARACTERS.sub("_", name)
345
+ base = name
346
+ while not name_allowed(name):
347
+ name = f"{base}_{next(self._counter)}"
348
+ return name
349
+ if not valid_identifier.fullmatch(name):
350
+ raise QASM3ExporterError(f"cannot use '{name}' as a name; it is not a valid identifier")
351
+ if name in _RESERVED_KEYWORDS:
352
+ raise QASM3ExporterError(f"cannot use the keyword '{name}' as a variable name")
353
+ if not name_allowed(name):
354
+ if self.gates.get(name) is not None:
355
+ raise QASM3ExporterError(
356
+ f"cannot shadow variable '{name}', as it is already defined as a gate"
357
+ )
358
+ for scope in reversed(self.variables):
359
+ if (other := scope.get(name)) is not None:
360
+ break
361
+ else: # pragma: no cover
362
+ raise RuntimeError(f"internal error: could not locate unshadowable '{name}'")
363
+ raise QASM3ExporterError(
364
+ f"cannot shadow variable '{name}', as it is already defined as '{other}'"
365
+ )
366
+ return name
367
+
368
+ def register_variable(
369
+ self,
370
+ name: str,
371
+ variable: object,
372
+ *,
373
+ allow_rename: bool,
374
+ force_global: bool = False,
375
+ allow_hardware_qubit: bool = False,
376
+ ) -> ast.Identifier:
377
+ """Register a variable in the symbol table for the given scope, returning the name that
378
+ should be used to refer to the variable. The same name will be returned by subsequent calls
379
+ to :meth:`get_variable` within the same scope.
380
+
381
+ Args:
382
+ name: the name to base the identifier on.
383
+ variable: the Qiskit object this refers to. This can be ``None`` in the case of
384
+ reserving a dummy variable name that does not actually have a Qiskit object backing
385
+ it.
386
+ allow_rename: whether to allow the name to be mutated to escape it and/or make it safe
387
+ to define (avoiding keywords, subject to shadowing rules, etc).
388
+ force_global: force this declaration to be in the global scope.
389
+ allow_hardware_qubit: whether to allow hardware qubits to pass through as identifiers.
390
+ Hardware qubits are a dollar sign followed by a non-negative integer, and cannot be
391
+ declared, so are not suitable identifiers for most objects.
392
+ """
393
+ scope_index = 0 if force_global else -1
394
+ # We still need to do this escaping and shadow checking if `force_global`, because we don't
395
+ # want a previous variable declared in the currently active scope to shadow the global.
396
+ # This logic would be cleaner if we made the naming choices later, after AST generation
397
+ # (e.g. by using only indices as the identifiers until we're outputting the program).
398
+ if allow_hardware_qubit and _VALID_HARDWARE_QUBIT.fullmatch(name):
399
+ if self.symbol_defined(name): # pragma: no cover
400
+ raise QASM3ExporterError(f"internal error: cannot redeclare hardware qubit {name}")
314
401
  else:
315
- key = instruction.name
316
- self[key] = instruction
402
+ name = self.escaped_declarable_name(
403
+ name, allow_rename=allow_rename, unique=force_global
404
+ )
405
+ identifier = ast.Identifier(name)
406
+ self.variables[scope_index][name] = variable
407
+ if variable is not None:
408
+ self.objects[scope_index][variable] = identifier
409
+ return identifier
410
+
411
+ def set_object_ident(self, ident: ast.Identifier, variable: object):
412
+ """Set the identifier used to refer to a given object for this scope.
317
413
 
414
+ This overwrites any previously set identifier, such as during the original registration.
318
415
 
319
- # A _Scope is the structure used in the builder to store the contexts and re-mappings of bits from
320
- # the top-level scope where the bits were actually defined. In the class, 'circuit' is an instance
321
- # of QuantumCircuit that defines this level, and 'bit_map' is a mapping of 'Bit: Bit', where the
322
- # keys are bits in the circuit in this scope, and the values are the Bit in the top-level scope in
323
- # this context that this bit actually represents. 'symbol_map' is a bidirectional mapping of
324
- # '<Terra object>: Identifier' and 'str: <Terra object>', where the string in the second map is the
325
- # name of the identifier. This is a cheap hack around actually implementing a proper symbol table.
326
- _Scope = collections.namedtuple("_Scope", ("circuit", "bit_map", "symbol_map"))
416
+ This is generally only useful for tracking "sub" objects, like bits out of a register, which
417
+ will have an `SubscriptedIdentifier` as their identifier."""
418
+ self.objects[-1][variable] = ident
419
+
420
+ def get_variable(self, variable: object) -> ast.Identifier:
421
+ """Lookup a non-gate variable in the symbol table."""
422
+ for scope in reversed(self.objects):
423
+ if (out := scope.get(variable)) is not None:
424
+ return out
425
+ raise KeyError(f"'{variable}' is not defined in the current context")
426
+
427
+ def register_gate_without_definition(self, name: str, gate: Gate | None) -> ast.Identifier:
428
+ """Register a gate that does not require an OQ3 definition.
429
+
430
+ If the ``gate`` is given, it will be used to validate that a call to it is compatible (such
431
+ as a known gate from an included file). If it is not given, it is treated as a user-defined
432
+ "basis gate" that assumes that all calling signatures are valid and that all gates of this
433
+ name are exactly compatible, which is somewhat dangerous."""
434
+ # Validate the name is usable.
435
+ name = self.escaped_declarable_name(name, allow_rename=False, unique=False)
436
+ ident = ast.Identifier(name)
437
+ if gate is None:
438
+ self.gates[name] = GateInfo(None, None)
439
+ else:
440
+ canonical = _gate_canonical_form(gate)
441
+ self.gates[name] = GateInfo(canonical, None)
442
+ if canonical._standard_gate is not None:
443
+ self.standard_gate_idents[canonical._standard_gate] = ident
444
+ else:
445
+ self.user_gate_idents[id(canonical)] = ident
446
+ return ident
447
+
448
+ def register_gate(
449
+ self,
450
+ name: str,
451
+ source: Gate,
452
+ params: Iterable[ast.Identifier],
453
+ qubits: Iterable[ast.Identifier],
454
+ body: ast.QuantumBlock,
455
+ ) -> ast.Identifier:
456
+ """Register the given gate in the symbol table, using the given components to build up the
457
+ full AST definition."""
458
+ name = self.escaped_declarable_name(name, allow_rename=True, unique=False)
459
+ ident = ast.Identifier(name)
460
+ self.gates[name] = GateInfo(
461
+ source, ast.QuantumGateDefinition(ident, tuple(params), tuple(qubits), body)
462
+ )
463
+ # Add the gate object with a magic lookup keep to the objects dictionary so we can retrieve
464
+ # it later. Standard gates are not guaranteed to have stable IDs (they're preferentially
465
+ # not even created in Python space), but user gates are.
466
+ if source._standard_gate is not None:
467
+ self.standard_gate_idents[source._standard_gate] = ident
468
+ else:
469
+ self.user_gate_idents[id(source)] = ident
470
+ return ident
471
+
472
+ def get_gate(self, gate: Gate) -> ast.Identifier | None:
473
+ """Lookup the identifier for a given `Gate`, if it exists."""
474
+ canonical = _gate_canonical_form(gate)
475
+ # `our_defn.canonical is None` means a basis gate that we should assume is always valid.
476
+ if (our_defn := self.gates.get(gate.name)) is not None and (
477
+ our_defn.canonical is None or our_defn.canonical == canonical
478
+ ):
479
+ return ast.Identifier(gate.name)
480
+ if canonical._standard_gate is not None:
481
+ if (our_ident := self.standard_gate_idents.get(canonical._standard_gate)) is None:
482
+ return None
483
+ return our_ident if self.gates[our_ident.string].canonical == canonical else None
484
+ # No need to check equality if we're looking up by `id`; we must have the same object.
485
+ return self.user_gate_idents.get(id(canonical))
486
+
487
+
488
+ def _gate_canonical_form(gate: Gate) -> Gate:
489
+ """Get the canonical form of a gate.
490
+
491
+ This is the gate object that should be used to provide the OpenQASM 3 definition of a gate (but
492
+ not the call site; that's the input object). This lets us return a re-parametrised gate in
493
+ terms of general parameters, in cases where we can be sure that that is valid. This is
494
+ currently only Qiskit standard gates. This lets multiple call-site gates match the same symbol,
495
+ in the case of parametric gates.
496
+
497
+ The definition source provides the number of qubits, the parameter signature and the body of the
498
+ `gate` statement. It does not provide the name of the symbol being defined."""
499
+ # If a gate is part of the Qiskit standard-library gates, we know we can safely produce a
500
+ # reparameterised gate by passing the parameters positionally to the standard-gate constructor
501
+ # (and control state, if appropriate).
502
+ if gate._standard_gate and not isinstance(gate, ControlledGate):
503
+ return gate.base_class(*_FIXED_PARAMETERS[: len(gate.params)])
504
+ elif gate._standard_gate:
505
+ return gate.base_class(*_FIXED_PARAMETERS[: len(gate.params)], ctrl_state=gate.ctrl_state)
506
+ return gate
507
+
508
+
509
+ @dataclasses.dataclass
510
+ class BuildScope:
511
+ """The structure used in the builder to store the contexts and re-mappings of bits from the
512
+ top-level scope where the bits were actually defined."""
513
+
514
+ circuit: QuantumCircuit
515
+ """The circuit block that we're currently working on exporting."""
516
+ bit_map: dict[Bit, Bit]
517
+ """Mapping of bit objects in ``circuit`` to the bit objects in the global-scope program
518
+ :class:`.QuantumCircuit` that they are bound to."""
327
519
 
328
520
 
329
521
  class QASM3Builder:
@@ -344,13 +536,11 @@ class QASM3Builder:
344
536
  allow_aliasing,
345
537
  experimental=ExperimentalFeatures(0),
346
538
  ):
347
- # This is a stack of stacks; the outer stack is a list of "outer" look-up contexts, and the
348
- # inner stack is for scopes within these. A "outer" look-up context in this sense means
349
- # the main program body or a gate/subroutine definition, whereas the scopes are for things
350
- # like the body of a ``for`` loop construct.
351
- self._circuit_ctx = []
352
- self.push_context(quantumcircuit)
353
- self.includeslist = includeslist
539
+ self.scope = BuildScope(
540
+ quantumcircuit,
541
+ {x: x for x in itertools.chain(quantumcircuit.qubits, quantumcircuit.clbits)},
542
+ )
543
+ self.symbols = SymbolTable()
354
544
  # `_global_io_declarations` and `_global_classical_declarations` are stateful, and any
355
545
  # operation that needs a parameter can append to them during the build. We make all
356
546
  # classical declarations global because the IBM qe-compiler stack (our initial consumer of
@@ -359,110 +549,88 @@ class QASM3Builder:
359
549
  # in the near term.
360
550
  self._global_io_declarations = []
361
551
  self._global_classical_forward_declarations = []
362
- # An arbitrary counter to help with generation of unique ids for symbol names when there are
363
- # clashes (though we generally prefer to keep user names if possible).
364
- self._counter = itertools.count()
365
552
  self.disable_constants = disable_constants
366
553
  self.allow_aliasing = allow_aliasing
367
- self.global_namespace = GlobalNamespace(includeslist, basis_gates)
554
+ self.includes = includeslist
555
+ self.basis_gates = basis_gates
368
556
  self.experimental = experimental
369
557
 
370
- def _unique_name(self, prefix: str, scope: _Scope) -> str:
371
- table = scope.symbol_map
372
- name = basename = _escape_invalid_identifier(prefix)
373
- while name in table or name in _RESERVED_KEYWORDS or self.global_namespace.has_symbol(name):
374
- name = f"{basename}__generated{next(self._counter)}"
375
- return name
376
-
377
- def _register_gate(self, gate):
378
- self.global_namespace.register(gate)
379
-
380
- def _register_opaque(self, instruction):
381
- self.global_namespace.register(instruction)
382
-
383
- def _register_variable(self, variable, scope: _Scope, name=None) -> ast.Identifier:
384
- """Register a variable in the symbol table for the given scope, returning the name that
385
- should be used to refer to the variable. The same name will be returned by subsequent calls
386
- to :meth:`_lookup_variable` within the same scope.
387
-
388
- If ``name`` is given explicitly, it must not already be defined in the scope.
389
- """
390
- # Note that the registration only checks for the existence of a variable that was declared
391
- # in the current scope, not just one that's available. This is a rough implementation of
392
- # the shadowing proposal currently being drafted for OpenQASM 3, though we expect it to be
393
- # expanded and modified in the future (2022-03-07).
394
- table = scope.symbol_map
395
- if name is not None:
396
- if name in _RESERVED_KEYWORDS:
397
- raise QASM3ExporterError(f"cannot reserve the keyword '{name}' as a variable name")
398
- if name in table:
399
- raise QASM3ExporterError(
400
- f"tried to reserve '{name}', but it is already used by '{table[name]}'"
401
- )
402
- if self.global_namespace.has_symbol(name):
403
- raise QASM3ExporterError(
404
- f"tried to reserve '{name}', but it is already used by a gate"
405
- )
406
- else:
407
- name = self._unique_name(variable.name, scope)
408
- identifier = ast.Identifier(name)
409
- table[identifier.string] = variable
410
- table[variable] = identifier
411
- return identifier
412
-
413
- def _reserve_variable_name(self, name: ast.Identifier, scope: _Scope) -> ast.Identifier:
414
- """Reserve a variable name in the given scope, raising a :class:`.QASM3ExporterError` if
415
- the name is already in use.
416
-
417
- This is useful for autogenerated names that the exporter itself reserves when dealing with
418
- objects that have no standard Terra object backing them.
419
-
420
- Returns the same identifier, for convenience in chaining."""
421
- table = scope.symbol_map
422
- if name.string in table:
423
- variable = table[name.string]
424
- raise QASM3ExporterError(
425
- f"tried to reserve '{name.string}', but it is already used by '{variable}'"
558
+ @contextlib.contextmanager
559
+ def new_scope(self, circuit: QuantumCircuit, qubits: Iterable[Qubit], clbits: Iterable[Clbit]):
560
+ """Context manager that pushes a new scope (like a ``for`` or ``while`` loop body) onto the
561
+ current context stack."""
562
+ current_map = self.scope.bit_map
563
+ qubits = tuple(current_map[qubit] for qubit in qubits)
564
+ clbits = tuple(current_map[clbit] for clbit in clbits)
565
+ if circuit.num_qubits != len(qubits):
566
+ raise QASM3ExporterError( # pragma: no cover
567
+ f"Tried to push a scope whose circuit needs {circuit.num_qubits} qubits, but only"
568
+ f" provided {len(qubits)} qubits to create the mapping."
426
569
  )
427
- table[name.string] = "<internal object>"
428
- return name
570
+ if circuit.num_clbits != len(clbits):
571
+ raise QASM3ExporterError( # pragma: no cover
572
+ f"Tried to push a scope whose circuit needs {circuit.num_clbits} clbits, but only"
573
+ f" provided {len(clbits)} clbits to create the mapping."
574
+ )
575
+ mapping = dict(itertools.chain(zip(circuit.qubits, qubits), zip(circuit.clbits, clbits)))
576
+ self.symbols.push_scope()
577
+ old_scope, self.scope = self.scope, BuildScope(circuit, mapping)
578
+ yield self.scope
579
+ self.scope = old_scope
580
+ self.symbols.pop_scope()
581
+
582
+ @contextlib.contextmanager
583
+ def new_context(self, body: QuantumCircuit):
584
+ """Push a new context (like for a ``gate`` or ``def`` body) onto the stack."""
585
+ mapping = {bit: bit for bit in itertools.chain(body.qubits, body.clbits)}
586
+
587
+ old_symbols, self.symbols = self.symbols, self.symbols.new_context()
588
+ old_scope, self.scope = self.scope, BuildScope(body, mapping)
589
+ yield self.scope
590
+ self.scope = old_scope
591
+ self.symbols = old_symbols
429
592
 
430
593
  def _lookup_variable(self, variable) -> ast.Identifier:
431
- """Lookup a Terra object within the current context, and return the name that should be used
432
- to represent it in OpenQASM 3 programmes."""
594
+ """Lookup a Qiskit object within the current context, and return the name that should be
595
+ used to represent it in OpenQASM 3 programmes."""
433
596
  if isinstance(variable, Bit):
434
- variable = self.current_scope().bit_map[variable]
435
- for scope in reversed(self.current_context()):
436
- if variable in scope.symbol_map:
437
- return scope.symbol_map[variable]
438
- raise KeyError(f"'{variable}' is not defined in the current context")
439
-
440
- def build_header(self):
441
- """Builds a Header"""
442
- version = ast.Version("3.0")
443
- includes = self.build_includes()
444
- return ast.Header(version, includes)
597
+ variable = self.scope.bit_map[variable]
598
+ return self.symbols.get_variable(variable)
445
599
 
446
600
  def build_program(self):
447
601
  """Builds a Program"""
448
- circuit = self.global_scope(assert_=True).circuit
602
+ circuit = self.scope.circuit
449
603
  if circuit.num_captured_vars:
450
604
  raise QASM3ExporterError(
451
605
  "cannot export an inner scope with captured variables as a top-level program"
452
606
  )
453
- header = self.build_header()
454
607
 
455
- opaques_to_declare, gates_to_declare = self.hoist_declarations(
456
- circuit.data, opaques=[], gates=[]
457
- )
458
- opaque_definitions = [
459
- self.build_opaque_definition(instruction) for instruction in opaques_to_declare
460
- ]
461
- gate_definitions = [
462
- self.build_gate_definition(instruction) for instruction in gates_to_declare
463
- ]
608
+ # The order we build parts of the AST has an effect on which names will get escaped to avoid
609
+ # collisions. The current ideas are:
610
+ #
611
+ # * standard-library include files _must_ define symbols of the correct name.
612
+ # * classical registers, IO variables and `Var` nodes are likely to be referred to by name
613
+ # by a user, so they get very high priority - we search for them before doing anything.
614
+ # * qubit registers are not typically referred to by name by users, so they get a lower
615
+ # priority than the classical variables.
616
+ # * we often have to escape user-defined gate names anyway because of our dodgy parameter
617
+ # handling, so they get the lowest priority; they get defined as they are encountered.
618
+ #
619
+ # An alternative approach would be to defer naming decisions until we are outputting the
620
+ # AST, and using some UUID for each symbol we're going to define in the interrim. This
621
+ # would require relatively large changes to the symbol-table and AST handling, however.
622
+
623
+ for builtin, gate in _BUILTIN_GATES.items():
624
+ self.symbols.register_gate_without_definition(builtin, gate)
625
+ for builtin in self.basis_gates:
626
+ if builtin in _BUILTIN_GATES:
627
+ # It's built into the langauge; we don't need to re-add it.
628
+ continue
629
+ self.symbols.register_gate_without_definition(builtin, None)
464
630
 
465
- # Early IBM runtime paramterisation uses unbound `Parameter` instances as `input` variables,
631
+ header = ast.Header(ast.Version("3.0"), list(self.build_includes()))
632
+
633
+ # Early IBM runtime parametrization uses unbound `Parameter` instances as `input` variables,
466
634
  # not the explicit realtime `Var` variables, so we need this explicit scan.
467
635
  self.hoist_global_parameter_declarations()
468
636
  # Qiskit's clbits and classical registers need to get mapped to implicit OQ3 variables, but
@@ -476,9 +644,11 @@ class QASM3Builder:
476
644
 
477
645
  # Similarly, QuantumCircuit qubits/registers are only new variables in the global scope.
478
646
  quantum_declarations = self.build_quantum_declarations()
647
+
479
648
  # This call has side-effects - it can populate `self._global_io_declarations` and
480
649
  # `self._global_classical_declarations` as a courtesy to the qe-compiler that prefers our
481
- # hacky temporary `switch` target variables to be globally defined.
650
+ # hacky temporary `switch` target variables to be globally defined. It also populates the
651
+ # symbol table with encountered gates that weren't previously defined.
482
652
  main_statements = self.build_current_scope()
483
653
 
484
654
  statements = [
@@ -487,8 +657,7 @@ class QASM3Builder:
487
657
  # In older versions of the reference OQ3 grammar, IO declarations had to come before
488
658
  # anything else, so we keep doing that as a courtesy.
489
659
  self._global_io_declarations,
490
- opaque_definitions,
491
- gate_definitions,
660
+ (gate.node for gate in self.symbols.gates.values() if gate.node is not None),
492
661
  self._global_classical_forward_declarations,
493
662
  quantum_declarations,
494
663
  main_statements,
@@ -497,172 +666,98 @@ class QASM3Builder:
497
666
  ]
498
667
  return ast.Program(header, statements)
499
668
 
500
- def hoist_declarations(self, instructions, *, opaques, gates):
501
- """Walks the definitions in gates/instructions to make a list of gates to declare.
502
-
503
- Mutates ``opaques`` and ``gates`` in-place if given, and returns them."""
504
- for instruction in instructions:
505
- if isinstance(instruction.operation, ControlFlowOp):
506
- for block in instruction.operation.blocks:
507
- self.hoist_declarations(block.data, opaques=opaques, gates=gates)
508
- continue
509
- if instruction.operation in self.global_namespace or isinstance(
510
- instruction.operation, self.builtins
511
- ):
512
- continue
513
-
514
- if isinstance(instruction.operation, standard_gates.CXGate):
515
- # CX gets super duper special treatment because it's the base of Terra's definition
516
- # tree, but isn't an OQ3 built-in. We use `isinstance` because we haven't fully
517
- # fixed what the name/class distinction is (there's a test from the original OQ3
518
- # exporter that tries a naming collision with 'cx').
519
- self._register_gate(instruction.operation)
520
- gates.append(instruction.operation)
521
- elif instruction.operation.definition is None:
522
- self._register_opaque(instruction.operation)
523
- opaques.append(instruction.operation)
524
- elif not isinstance(instruction.operation, Gate):
525
- raise QASM3ExporterError("Exporting non-unitary instructions is not yet supported.")
526
- else:
527
- self.hoist_declarations(
528
- instruction.operation.definition.data, opaques=opaques, gates=gates
529
- )
530
- self._register_gate(instruction.operation)
531
- gates.append(instruction.operation)
532
- return opaques, gates
533
-
534
- def global_scope(self, assert_=False):
535
- """Return the global circuit scope that is used as the basis of the full program. If
536
- ``assert_=True``, then this raises :obj:`.QASM3ExporterError` if the current context is not
537
- the global one."""
538
- if assert_ and len(self._circuit_ctx) != 1 and len(self._circuit_ctx[0]) != 1:
539
- # Defensive code to help catch logic errors.
540
- raise QASM3ExporterError( # pragma: no cover
541
- f"Not currently in the global context. Current contexts are: {self._circuit_ctx}"
542
- )
543
- return self._circuit_ctx[0][0]
544
-
545
- def current_scope(self):
546
- """Return the current circuit scope."""
547
- return self._circuit_ctx[-1][-1]
548
-
549
- def current_context(self):
550
- """Return the current context (list of scopes)."""
551
- return self._circuit_ctx[-1]
552
-
553
- def push_scope(self, circuit: QuantumCircuit, qubits: Iterable[Qubit], clbits: Iterable[Clbit]):
554
- """Push a new scope (like a ``for`` or ``while`` loop body) onto the current context
555
- stack."""
556
- current_map = self.current_scope().bit_map
557
- qubits = tuple(current_map[qubit] for qubit in qubits)
558
- clbits = tuple(current_map[clbit] for clbit in clbits)
559
- if circuit.num_qubits != len(qubits):
560
- raise QASM3ExporterError( # pragma: no cover
561
- f"Tried to push a scope whose circuit needs {circuit.num_qubits} qubits, but only"
562
- f" provided {len(qubits)} qubits to create the mapping."
563
- )
564
- if circuit.num_clbits != len(clbits):
565
- raise QASM3ExporterError( # pragma: no cover
566
- f"Tried to push a scope whose circuit needs {circuit.num_clbits} clbits, but only"
567
- f" provided {len(clbits)} clbits to create the mapping."
568
- )
569
- mapping = dict(itertools.chain(zip(circuit.qubits, qubits), zip(circuit.clbits, clbits)))
570
- self.current_context().append(_Scope(circuit, mapping, {}))
571
-
572
- def pop_scope(self) -> _Scope:
573
- """Pop the current scope (like a ``for`` or ``while`` loop body) off the current context
574
- stack."""
575
- if len(self._circuit_ctx[-1]) <= 1:
576
- raise QASM3ExporterError( # pragma: no cover
577
- "Tried to pop a scope from the current context, but there are no current scopes."
578
- )
579
- return self._circuit_ctx[-1].pop()
580
-
581
- def push_context(self, outer_context: QuantumCircuit):
582
- """Push a new context (like for a ``gate`` or ``def`` body) onto the stack."""
583
- mapping = {bit: bit for bit in itertools.chain(outer_context.qubits, outer_context.clbits)}
584
- self._circuit_ctx.append([_Scope(outer_context, mapping, {})])
585
-
586
- def pop_context(self):
587
- """Pop the current context (like for a ``gate`` or ``def`` body) onto the stack."""
588
- if len(self._circuit_ctx) == 1:
589
- raise QASM3ExporterError( # pragma: no cover
590
- "Tried to pop the current context, but that is the global context."
591
- )
592
- if len(self._circuit_ctx[-1]) != 1:
593
- raise QASM3ExporterError( # pragma: no cover
594
- "Tried to pop the current context while there are still"
595
- f" {len(self._circuit_ctx[-1]) - 1} unclosed scopes."
596
- )
597
- self._circuit_ctx.pop()
598
-
599
669
  def build_includes(self):
600
670
  """Builds a list of included files."""
601
- return [ast.Include(filename) for filename in self.includeslist]
602
-
603
- def build_opaque_definition(self, instruction):
604
- """Builds an Opaque gate definition as a CalibrationDefinition"""
605
- # We can't do anything sensible with this yet, so it's better to loudly say that.
606
- raise QASM3ExporterError(
607
- "Exporting opaque instructions with pulse-level calibrations is not yet supported by"
608
- " the OpenQASM 3 exporter. Received this instruction, which appears opaque:"
609
- f"\n{instruction}"
610
- )
611
-
612
- def build_gate_definition(self, gate):
613
- """Builds a QuantumGateDefinition"""
614
- if isinstance(gate, standard_gates.CXGate):
615
- # CX gets super duper special treatment because it's the base of Terra's definition
616
- # tree, but isn't an OQ3 built-in. We use `isinstance` because we haven't fully
617
- # fixed what the name/class distinction is (there's a test from the original OQ3
618
- # exporter that tries a naming collision with 'cx').
671
+ for filename in self.includes:
672
+ if (definitions := _KNOWN_INCLUDES.get(filename)) is None:
673
+ raise QASM3ExporterError(f"Unknown OpenQASM 3 include file: '{filename}'")
674
+ for name, gate in definitions.items():
675
+ self.symbols.register_gate_without_definition(name, gate)
676
+ yield ast.Include(filename)
677
+
678
+ def define_gate(self, gate: Gate) -> ast.Identifier:
679
+ """Define a gate in the symbol table, including building the gate-definition statement for
680
+ it.
681
+
682
+ This recurses through gate-definition statements."""
683
+ if issubclass(gate.base_class, library.CXGate) and gate.ctrl_state == 1:
684
+ # CX gets super duper special treatment because it's the base of Qiskit's definition
685
+ # tree, but isn't an OQ3 built-in (it was in OQ2). We use `issubclass` because we
686
+ # haven't fully fixed what the name/class distinction is (there's a test from the
687
+ # original OQ3 exporter that tries a naming collision with 'cx').
619
688
  control, target = ast.Identifier("c"), ast.Identifier("t")
620
- call = ast.QuantumGateCall(
621
- ast.Identifier("U"),
622
- [control, target],
623
- parameters=[ast.Constant.PI, ast.IntegerLiteral(0), ast.Constant.PI],
624
- modifiers=[ast.QuantumGateModifier(ast.QuantumGateModifierName.CTRL)],
625
- )
626
- return ast.QuantumGateDefinition(
627
- ast.QuantumGateSignature(ast.Identifier("cx"), [control, target]),
628
- ast.QuantumBlock([call]),
689
+ body = ast.QuantumBlock(
690
+ [
691
+ ast.QuantumGateCall(
692
+ self.symbols.get_gate(library.UGate(math.pi, 0, math.pi)),
693
+ [control, target],
694
+ parameters=[ast.Constant.PI, ast.IntegerLiteral(0), ast.Constant.PI],
695
+ modifiers=[ast.QuantumGateModifier(ast.QuantumGateModifierName.CTRL)],
696
+ )
697
+ ]
629
698
  )
699
+ return self.symbols.register_gate(gate.name, gate, (), (control, target), body)
700
+ if gate.definition is None:
701
+ raise QASM3ExporterError(f"failed to export gate '{gate.name}' that has no definition")
702
+ canonical = _gate_canonical_form(gate)
703
+ with self.new_context(canonical.definition):
704
+ defn = self.scope.circuit
705
+ # If `defn.num_parameters == 0` but `gate.params` is non-empty, we are likely in the
706
+ # case where the gate's circuit definition is fully bound (so we can't detect its inputs
707
+ # anymore). This is a problem in our data model - for arbitrary user gates, there's no
708
+ # way we can reliably get a parametric version of the gate through our interfaces. In
709
+ # this case, we output a gate that has dummy parameters, and rely on it being a
710
+ # different `id` each time to avoid duplication. We assume that the parametrisation
711
+ # order matches (which is a _big_ assumption).
712
+ #
713
+ # If `defn.num_parameters > 0`, we enforce that it must match how it's called.
714
+ if defn.num_parameters > 0:
715
+ if defn.num_parameters != len(gate.params):
716
+ raise QASM3ExporterError(
717
+ "parameter mismatch in definition of '{gate}':"
718
+ f" call has {len(gate.params)}, definition has {defn.num_parameters}"
719
+ )
720
+ params = [
721
+ self.symbols.register_variable(param.name, param, allow_rename=True)
722
+ for param in defn.parameters
723
+ ]
724
+ else:
725
+ # Fill with dummy parameters. The name is unimportant, because they're not actually
726
+ # used in the definition.
727
+ params = [
728
+ self.symbols.register_variable(
729
+ f"{self.gate_parameter_prefix}_{i}", None, allow_rename=True
730
+ )
731
+ for i in range(len(gate.params))
732
+ ]
733
+ qubits = [
734
+ self.symbols.register_variable(
735
+ f"{self.gate_qubit_prefix}_{i}", qubit, allow_rename=True
736
+ )
737
+ for i, qubit in enumerate(defn.qubits)
738
+ ]
739
+ body = ast.QuantumBlock(self.build_current_scope())
740
+ # We register the gate only after building its body so that any gates we needed for that in
741
+ # turn are registered in the correct order. Gates can't be recursive in OQ3, so there's no
742
+ # problem with delaying this.
743
+ return self.symbols.register_gate(canonical.name, canonical, params, qubits, body)
630
744
 
631
- self.push_context(gate.definition)
632
- signature = self.build_gate_signature(gate)
633
- body = ast.QuantumBlock(self.build_current_scope())
634
- self.pop_context()
635
- return ast.QuantumGateDefinition(signature, body)
636
-
637
- def build_gate_signature(self, gate):
638
- """Builds a QuantumGateSignature"""
639
- name = self.global_namespace[gate]
640
- params = []
641
- definition = gate.definition
642
- # Dummy parameters
643
- scope = self.current_scope()
644
- for num in range(len(gate.params) - len(definition.parameters)):
645
- param_name = f"{self.gate_parameter_prefix}_{num}"
646
- params.append(self._reserve_variable_name(ast.Identifier(param_name), scope))
647
- params += [self._register_variable(param, scope) for param in definition.parameters]
648
- quantum_arguments = [
649
- self._register_variable(
650
- qubit, scope, self._unique_name(f"{self.gate_qubit_prefix}_{i}", scope)
651
- )
652
- for i, qubit in enumerate(definition.qubits)
653
- ]
654
- return ast.QuantumGateSignature(ast.Identifier(name), quantum_arguments, params or None)
745
+ def assert_global_scope(self):
746
+ """Raise an error if we are not in the global scope, as a defensive measure."""
747
+ if len(self.symbols.variables) > 1: # pragma: no cover
748
+ raise RuntimeError("not currently in the global scope")
655
749
 
656
750
  def hoist_global_parameter_declarations(self):
657
751
  """Extend ``self._global_io_declarations`` and ``self._global_classical_declarations`` with
658
752
  any implicit declarations used to support the early IBM efforts to use :class:`.Parameter`
659
753
  as an input variable."""
660
- global_scope = self.global_scope(assert_=True)
661
- for parameter in global_scope.circuit.parameters:
662
- parameter_name = self._register_variable(parameter, global_scope)
663
- declaration = _infer_variable_declaration(
664
- global_scope.circuit, parameter, parameter_name
754
+ self.assert_global_scope()
755
+ circuit = self.scope.circuit
756
+ for parameter in circuit.parameters:
757
+ parameter_name = self.symbols.register_variable(
758
+ parameter.name, parameter, allow_rename=True
665
759
  )
760
+ declaration = _infer_variable_declaration(circuit, parameter, parameter_name)
666
761
  if declaration is None:
667
762
  continue
668
763
  if isinstance(declaration, ast.IODeclaration):
@@ -676,15 +771,16 @@ class QASM3Builder:
676
771
  doesn't involve the declaration of *new* bits or registers in inner scopes; only the
677
772
  :class:`.expr.Var` mechanism allows that.
678
773
 
679
- The behaviour of this function depends on the setting ``allow_aliasing``. If this
774
+ The behavior of this function depends on the setting ``allow_aliasing``. If this
680
775
  is ``True``, then the output will be in the same form as the output of
681
776
  :meth:`.build_classical_declarations`, with the registers being aliases. If ``False``, it
682
777
  will instead return a :obj:`.ast.ClassicalDeclaration` for each classical register, and one
683
778
  for the loose :obj:`.Clbit` instances, and will raise :obj:`QASM3ExporterError` if any
684
779
  registers overlap.
685
780
  """
686
- scope = self.global_scope(assert_=True)
687
- if any(len(scope.circuit.find_bit(q).registers) > 1 for q in scope.circuit.clbits):
781
+ self.assert_global_scope()
782
+ circuit = self.scope.circuit
783
+ if any(len(circuit.find_bit(q).registers) > 1 for q in circuit.clbits):
688
784
  # There are overlapping registers, so we need to use aliases to emit the structure.
689
785
  if not self.allow_aliasing:
690
786
  raise QASM3ExporterError(
@@ -694,34 +790,32 @@ class QASM3Builder:
694
790
  clbits = (
695
791
  ast.ClassicalDeclaration(
696
792
  ast.BitType(),
697
- self._register_variable(
698
- clbit, scope, self._unique_name(f"{self.loose_bit_prefix}{i}", scope)
793
+ self.symbols.register_variable(
794
+ f"{self.loose_bit_prefix}{i}", clbit, allow_rename=True
699
795
  ),
700
796
  )
701
- for i, clbit in enumerate(scope.circuit.clbits)
797
+ for i, clbit in enumerate(circuit.clbits)
702
798
  )
703
799
  self._global_classical_forward_declarations.extend(clbits)
704
- self._global_classical_forward_declarations.extend(
705
- self.build_aliases(scope.circuit.cregs)
706
- )
800
+ self._global_classical_forward_declarations.extend(self.build_aliases(circuit.cregs))
707
801
  return
708
802
  # If we're here, we're in the clbit happy path where there are no clbits that are in more
709
803
  # than one register. We can output things very naturally.
710
804
  self._global_classical_forward_declarations.extend(
711
805
  ast.ClassicalDeclaration(
712
806
  ast.BitType(),
713
- self._register_variable(
714
- clbit, scope, self._unique_name(f"{self.loose_bit_prefix}{i}", scope)
807
+ self.symbols.register_variable(
808
+ f"{self.loose_bit_prefix}{i}", clbit, allow_rename=True
715
809
  ),
716
810
  )
717
- for i, clbit in enumerate(scope.circuit.clbits)
718
- if not scope.circuit.find_bit(clbit).registers
811
+ for i, clbit in enumerate(circuit.clbits)
812
+ if not circuit.find_bit(clbit).registers
719
813
  )
720
- for register in scope.circuit.cregs:
721
- name = self._register_variable(register, scope)
814
+ for register in circuit.cregs:
815
+ name = self.symbols.register_variable(register.name, register, allow_rename=True)
722
816
  for i, bit in enumerate(register):
723
- scope.symbol_map[bit] = ast.SubscriptedIdentifier(
724
- name.string, ast.IntegerLiteral(i)
817
+ self.symbols.set_object_ident(
818
+ ast.SubscriptedIdentifier(name.string, ast.IntegerLiteral(i)), bit
725
819
  )
726
820
  self._global_classical_forward_declarations.append(
727
821
  ast.ClassicalDeclaration(ast.BitArrayType(len(register)), name)
@@ -733,27 +827,31 @@ class QASM3Builder:
733
827
  Local :class:`.expr.Var` declarations are handled by the regular local-block scope builder,
734
828
  and the :class:`.QuantumCircuit` data model ensures that the only time an IO variable can
735
829
  occur is in an outermost block."""
736
- scope = self.global_scope(assert_=True)
737
- for var in scope.circuit.iter_input_vars():
830
+ self.assert_global_scope()
831
+ circuit = self.scope.circuit
832
+ for var in circuit.iter_input_vars():
738
833
  self._global_io_declarations.append(
739
834
  ast.IODeclaration(
740
835
  ast.IOModifier.INPUT,
741
836
  _build_ast_type(var.type),
742
- self._register_variable(var, scope),
837
+ self.symbols.register_variable(var.name, var, allow_rename=True),
743
838
  )
744
839
  )
745
840
 
746
841
  def build_quantum_declarations(self):
747
842
  """Return a list of AST nodes declaring all the qubits in the current scope, and all the
748
843
  alias declarations for these qubits."""
749
- scope = self.global_scope(assert_=True)
750
- if scope.circuit.layout is not None:
844
+ self.assert_global_scope()
845
+ circuit = self.scope.circuit
846
+ if circuit.layout is not None:
751
847
  # We're referring to physical qubits. These can't be declared in OQ3, but we need to
752
848
  # track the bit -> expression mapping in our symbol table.
753
- for i, bit in enumerate(scope.circuit.qubits):
754
- scope.symbol_map[bit] = ast.Identifier(f"${i}")
849
+ for i, bit in enumerate(circuit.qubits):
850
+ self.symbols.register_variable(
851
+ f"${i}", bit, allow_rename=False, allow_hardware_qubit=True
852
+ )
755
853
  return []
756
- if any(len(scope.circuit.find_bit(q).registers) > 1 for q in scope.circuit.qubits):
854
+ if any(len(circuit.find_bit(q).registers) > 1 for q in circuit.qubits):
757
855
  # There are overlapping registers, so we need to use aliases to emit the structure.
758
856
  if not self.allow_aliasing:
759
857
  raise QASM3ExporterError(
@@ -762,30 +860,30 @@ class QASM3Builder:
762
860
  )
763
861
  qubits = [
764
862
  ast.QuantumDeclaration(
765
- self._register_variable(
766
- qubit, scope, self._unique_name(f"{self.loose_qubit_prefix}{i}", scope)
863
+ self.symbols.register_variable(
864
+ f"{self.loose_qubit_prefix}{i}", qubit, allow_rename=True
767
865
  )
768
866
  )
769
- for i, qubit in enumerate(scope.circuit.qubits)
867
+ for i, qubit in enumerate(circuit.qubits)
770
868
  ]
771
- return qubits + self.build_aliases(scope.circuit.qregs)
869
+ return qubits + self.build_aliases(circuit.qregs)
772
870
  # If we're here, we're in the virtual-qubit happy path where there are no qubits that are in
773
871
  # more than one register. We can output things very naturally.
774
872
  loose_qubits = [
775
873
  ast.QuantumDeclaration(
776
- self._register_variable(
777
- qubit, scope, self._unique_name(f"{self.loose_qubit_prefix}{i}", scope)
874
+ self.symbols.register_variable(
875
+ f"{self.loose_qubit_prefix}{i}", qubit, allow_rename=True
778
876
  )
779
877
  )
780
- for i, qubit in enumerate(scope.circuit.qubits)
781
- if not scope.circuit.find_bit(qubit).registers
878
+ for i, qubit in enumerate(circuit.qubits)
879
+ if not circuit.find_bit(qubit).registers
782
880
  ]
783
881
  registers = []
784
- for register in scope.circuit.qregs:
785
- name = self._register_variable(register, scope)
882
+ for register in circuit.qregs:
883
+ name = self.symbols.register_variable(register.name, register, allow_rename=True)
786
884
  for i, bit in enumerate(register):
787
- scope.symbol_map[bit] = ast.SubscriptedIdentifier(
788
- name.string, ast.IntegerLiteral(i)
885
+ self.symbols.set_object_ident(
886
+ ast.SubscriptedIdentifier(name.string, ast.IntegerLiteral(i)), bit
789
887
  )
790
888
  registers.append(
791
889
  ast.QuantumDeclaration(name, ast.Designator(ast.IntegerLiteral(len(register))))
@@ -795,15 +893,14 @@ class QASM3Builder:
795
893
  def build_aliases(self, registers: Iterable[Register]) -> List[ast.AliasStatement]:
796
894
  """Return a list of alias declarations for the given registers. The registers can be either
797
895
  classical or quantum."""
798
- scope = self.current_scope()
799
896
  out = []
800
897
  for register in registers:
801
- name = self._register_variable(register, scope)
898
+ name = self.symbols.register_variable(register.name, register, allow_rename=True)
802
899
  elements = [self._lookup_variable(bit) for bit in register]
803
900
  for i, bit in enumerate(register):
804
901
  # This might shadow previous definitions, but that's not a problem.
805
- scope.symbol_map[bit] = ast.SubscriptedIdentifier(
806
- name.string, ast.IntegerLiteral(i)
902
+ self.symbols.set_object_ident(
903
+ ast.SubscriptedIdentifier(name.string, ast.IntegerLiteral(i)), bit
807
904
  )
808
905
  out.append(ast.AliasStatement(name, ast.IndexSet(elements)))
809
906
  return out
@@ -814,7 +911,6 @@ class QASM3Builder:
814
911
  In addition to everything literally in the circuit's ``data`` field, this also includes
815
912
  declarations for any local :class:`.expr.Var` nodes.
816
913
  """
817
- scope = self.current_scope()
818
914
 
819
915
  # We forward-declare all local variables uninitialised at the top of their scope. It would
820
916
  # be nice to declare the variable at the point of first store (so we can write things like
@@ -824,10 +920,13 @@ class QASM3Builder:
824
920
  # variable, or the initial write to a variable is within a control-flow scope. (It would be
825
921
  # easier to see the def/use chain needed to do this cleanly if we were using `DAGCircuit`.)
826
922
  statements = [
827
- ast.ClassicalDeclaration(_build_ast_type(var.type), self._register_variable(var, scope))
828
- for var in scope.circuit.iter_declared_vars()
923
+ ast.ClassicalDeclaration(
924
+ _build_ast_type(var.type),
925
+ self.symbols.register_variable(var.name, var, allow_rename=True),
926
+ )
927
+ for var in self.scope.circuit.iter_declared_vars()
829
928
  ]
830
- for instruction in scope.circuit.data:
929
+ for instruction in self.scope.circuit.data:
831
930
  if isinstance(instruction.operation, ControlFlowOp):
832
931
  if isinstance(instruction.operation, ForLoopOp):
833
932
  statements.append(self.build_for_loop(instruction))
@@ -871,7 +970,10 @@ class QASM3Builder:
871
970
  elif isinstance(instruction.operation, ContinueLoopOp):
872
971
  nodes = [ast.ContinueStatement()]
873
972
  else:
874
- nodes = [self.build_subroutine_call(instruction)]
973
+ raise QASM3ExporterError(
974
+ "non-unitary subroutine calls are not yet supported,"
975
+ f" but received '{instruction.operation}'"
976
+ )
875
977
 
876
978
  if instruction.operation.condition is None:
877
979
  statements.extend(nodes)
@@ -890,24 +992,21 @@ class QASM3Builder:
890
992
  condition = self.build_expression(_lift_condition(instruction.operation.condition))
891
993
 
892
994
  true_circuit = instruction.operation.blocks[0]
893
- self.push_scope(true_circuit, instruction.qubits, instruction.clbits)
894
- true_body = ast.ProgramBlock(self.build_current_scope())
895
- self.pop_scope()
995
+ with self.new_scope(true_circuit, instruction.qubits, instruction.clbits):
996
+ true_body = ast.ProgramBlock(self.build_current_scope())
896
997
  if len(instruction.operation.blocks) == 1:
897
998
  return ast.BranchingStatement(condition, true_body, None)
898
999
 
899
1000
  false_circuit = instruction.operation.blocks[1]
900
- self.push_scope(false_circuit, instruction.qubits, instruction.clbits)
901
- false_body = ast.ProgramBlock(self.build_current_scope())
902
- self.pop_scope()
1001
+ with self.new_scope(false_circuit, instruction.qubits, instruction.clbits):
1002
+ false_body = ast.ProgramBlock(self.build_current_scope())
903
1003
  return ast.BranchingStatement(condition, true_body, false_body)
904
1004
 
905
1005
  def build_switch_statement(self, instruction: CircuitInstruction) -> Iterable[ast.Statement]:
906
1006
  """Build a :obj:`.SwitchCaseOp` into a :class:`.ast.SwitchStatement`."""
907
1007
  real_target = self.build_expression(expr.lift(instruction.operation.target))
908
- global_scope = self.global_scope()
909
- target = self._reserve_variable_name(
910
- ast.Identifier(self._unique_name("switch_dummy", global_scope)), global_scope
1008
+ target = self.symbols.register_variable(
1009
+ "switch_dummy", None, allow_rename=True, force_global=True
911
1010
  )
912
1011
  self._global_classical_forward_declarations.append(
913
1012
  ast.ClassicalDeclaration(ast.IntType(), target, None)
@@ -921,9 +1020,8 @@ class QASM3Builder:
921
1020
  ast.DefaultCase() if v is CASE_DEFAULT else self.build_integer(v)
922
1021
  for v in values
923
1022
  ]
924
- self.push_scope(case_block, instruction.qubits, instruction.clbits)
925
- case_body = ast.ProgramBlock(self.build_current_scope())
926
- self.pop_scope()
1023
+ with self.new_scope(case_block, instruction.qubits, instruction.clbits):
1024
+ case_body = ast.ProgramBlock(self.build_current_scope())
927
1025
  return values, case_body
928
1026
 
929
1027
  return [
@@ -937,13 +1035,12 @@ class QASM3Builder:
937
1035
  ),
938
1036
  ]
939
1037
 
940
- # Handle the stabilised syntax.
1038
+ # Handle the stabilized syntax.
941
1039
  cases = []
942
1040
  default = None
943
1041
  for values, block in instruction.operation.cases_specifier():
944
- self.push_scope(block, instruction.qubits, instruction.clbits)
945
- case_body = ast.ProgramBlock(self.build_current_scope())
946
- self.pop_scope()
1042
+ with self.new_scope(block, instruction.qubits, instruction.clbits):
1043
+ case_body = ast.ProgramBlock(self.build_current_scope())
947
1044
  if CASE_DEFAULT in values:
948
1045
  # Even if it's mixed in with other cases, we can skip them and only output the
949
1046
  # `default` since that's valid and execution will be the same; the evaluation of
@@ -961,39 +1058,34 @@ class QASM3Builder:
961
1058
  """Build a :obj:`.WhileLoopOp` into a :obj:`.ast.WhileLoopStatement`."""
962
1059
  condition = self.build_expression(_lift_condition(instruction.operation.condition))
963
1060
  loop_circuit = instruction.operation.blocks[0]
964
- self.push_scope(loop_circuit, instruction.qubits, instruction.clbits)
965
- loop_body = ast.ProgramBlock(self.build_current_scope())
966
- self.pop_scope()
1061
+ with self.new_scope(loop_circuit, instruction.qubits, instruction.clbits):
1062
+ loop_body = ast.ProgramBlock(self.build_current_scope())
967
1063
  return ast.WhileLoopStatement(condition, loop_body)
968
1064
 
969
1065
  def build_for_loop(self, instruction: CircuitInstruction) -> ast.ForLoopStatement:
970
1066
  """Build a :obj:`.ForLoopOp` into a :obj:`.ast.ForLoopStatement`."""
971
1067
  indexset, loop_parameter, loop_circuit = instruction.operation.params
972
- self.push_scope(loop_circuit, instruction.qubits, instruction.clbits)
973
- scope = self.current_scope()
974
- if loop_parameter is None:
975
- # The loop parameter is implicitly declared by the ``for`` loop (see also
976
- # _infer_parameter_declaration), so it doesn't matter that we haven't declared this.
977
- loop_parameter_ast = self._reserve_variable_name(ast.Identifier("_"), scope)
978
- else:
979
- loop_parameter_ast = self._register_variable(loop_parameter, scope)
980
- if isinstance(indexset, range):
981
- # OpenQASM 3 uses inclusive ranges on both ends, unlike Python.
982
- indexset_ast = ast.Range(
983
- start=self.build_integer(indexset.start),
984
- end=self.build_integer(indexset.stop - 1),
985
- step=self.build_integer(indexset.step) if indexset.step != 1 else None,
1068
+ with self.new_scope(loop_circuit, instruction.qubits, instruction.clbits):
1069
+ name = "_" if loop_parameter is None else loop_parameter.name
1070
+ loop_parameter_ast = self.symbols.register_variable(
1071
+ name, loop_parameter, allow_rename=True
986
1072
  )
987
- else:
988
- try:
989
- indexset_ast = ast.IndexSet([self.build_integer(value) for value in indexset])
990
- except QASM3ExporterError:
991
- raise QASM3ExporterError(
992
- "The values in OpenQASM 3 'for' loops must all be integers, but received"
993
- f" '{indexset}'."
994
- ) from None
995
- body_ast = ast.ProgramBlock(self.build_current_scope())
996
- self.pop_scope()
1073
+ if isinstance(indexset, range):
1074
+ # OpenQASM 3 uses inclusive ranges on both ends, unlike Python.
1075
+ indexset_ast = ast.Range(
1076
+ start=self.build_integer(indexset.start),
1077
+ end=self.build_integer(indexset.stop - 1),
1078
+ step=self.build_integer(indexset.step) if indexset.step != 1 else None,
1079
+ )
1080
+ else:
1081
+ try:
1082
+ indexset_ast = ast.IndexSet([self.build_integer(value) for value in indexset])
1083
+ except QASM3ExporterError:
1084
+ raise QASM3ExporterError(
1085
+ "The values in OpenQASM 3 'for' loops must all be integers, but received"
1086
+ f" '{indexset}'."
1087
+ ) from None
1088
+ body_ast = ast.ProgramBlock(self.build_current_scope())
997
1089
  return ast.ForLoopStatement(indexset_ast, loop_parameter_ast, body_ast)
998
1090
 
999
1091
  def build_expression(self, node: expr.Expr) -> ast.Expression:
@@ -1048,11 +1140,13 @@ class QASM3Builder:
1048
1140
  )
1049
1141
 
1050
1142
  def build_gate_call(self, instruction: CircuitInstruction):
1051
- """Builds a QuantumGateCall"""
1052
- if isinstance(instruction.operation, standard_gates.UGate):
1053
- gate_name = ast.Identifier("U")
1054
- else:
1055
- gate_name = ast.Identifier(self.global_namespace[instruction.operation])
1143
+ """Builds a gate-call AST node.
1144
+
1145
+ This will also push the gate into the symbol table (if required), including recursively
1146
+ defining the gate blocks."""
1147
+ ident = self.symbols.get_gate(instruction.operation)
1148
+ if ident is None:
1149
+ ident = self.define_gate(instruction.operation)
1056
1150
  qubits = [self._lookup_variable(qubit) for qubit in instruction.qubits]
1057
1151
  if self.disable_constants:
1058
1152
  parameters = [
@@ -1065,7 +1159,7 @@ class QASM3Builder:
1065
1159
  for param in instruction.operation.params
1066
1160
  ]
1067
1161
 
1068
- return ast.QuantumGateCall(gate_name, qubits, parameters=parameters)
1162
+ return ast.QuantumGateCall(ident, qubits, parameters=parameters)
1069
1163
 
1070
1164
 
1071
1165
  def _infer_variable_declaration(
@@ -1102,7 +1196,10 @@ def _infer_variable_declaration(
1102
1196
  # _should_ be an intrinsic part of the parameter, or somewhere publicly accessible, but
1103
1197
  # Terra doesn't have those concepts yet. We can only try and guess at the type by looking
1104
1198
  # at all the places it's used in the circuit.
1105
- for instruction, index in circuit._parameter_table[parameter]:
1199
+ for instr_index, index in circuit._data._raw_parameter_table_entry(parameter):
1200
+ if instr_index is None:
1201
+ continue
1202
+ instruction = circuit.data[instr_index].operation
1106
1203
  if isinstance(instruction, ForLoopOp):
1107
1204
  # The parameters of ForLoopOp are (indexset, loop_parameter, body).
1108
1205
  if index == 1: