qiskit 2.0.3__cp39-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.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 (690) hide show
  1. qiskit/VERSION.txt +1 -0
  2. qiskit/__init__.py +141 -0
  3. qiskit/_accelerate.abi3.so +0 -0
  4. qiskit/_numpy_compat.py +73 -0
  5. qiskit/circuit/__init__.py +1343 -0
  6. qiskit/circuit/_add_control.py +312 -0
  7. qiskit/circuit/_classical_resource_map.py +150 -0
  8. qiskit/circuit/_standard_gates_commutations.py +3849 -0
  9. qiskit/circuit/_utils.py +167 -0
  10. qiskit/circuit/annotated_operation.py +279 -0
  11. qiskit/circuit/barrier.py +46 -0
  12. qiskit/circuit/classical/__init__.py +41 -0
  13. qiskit/circuit/classical/expr/__init__.py +266 -0
  14. qiskit/circuit/classical/expr/constructors.py +764 -0
  15. qiskit/circuit/classical/expr/expr.py +498 -0
  16. qiskit/circuit/classical/expr/visitors.py +375 -0
  17. qiskit/circuit/classical/types/__init__.py +113 -0
  18. qiskit/circuit/classical/types/ordering.py +229 -0
  19. qiskit/circuit/classical/types/types.py +153 -0
  20. qiskit/circuit/commutation_checker.py +133 -0
  21. qiskit/circuit/commutation_library.py +20 -0
  22. qiskit/circuit/controlflow/__init__.py +59 -0
  23. qiskit/circuit/controlflow/_builder_utils.py +211 -0
  24. qiskit/circuit/controlflow/box.py +163 -0
  25. qiskit/circuit/controlflow/break_loop.py +56 -0
  26. qiskit/circuit/controlflow/builder.py +791 -0
  27. qiskit/circuit/controlflow/continue_loop.py +56 -0
  28. qiskit/circuit/controlflow/control_flow.py +94 -0
  29. qiskit/circuit/controlflow/for_loop.py +218 -0
  30. qiskit/circuit/controlflow/if_else.py +498 -0
  31. qiskit/circuit/controlflow/switch_case.py +411 -0
  32. qiskit/circuit/controlflow/while_loop.py +166 -0
  33. qiskit/circuit/controlledgate.py +274 -0
  34. qiskit/circuit/delay.py +157 -0
  35. qiskit/circuit/duration.py +80 -0
  36. qiskit/circuit/equivalence.py +94 -0
  37. qiskit/circuit/equivalence_library.py +18 -0
  38. qiskit/circuit/exceptions.py +19 -0
  39. qiskit/circuit/gate.py +261 -0
  40. qiskit/circuit/instruction.py +564 -0
  41. qiskit/circuit/instructionset.py +132 -0
  42. qiskit/circuit/library/__init__.py +984 -0
  43. qiskit/circuit/library/arithmetic/__init__.py +40 -0
  44. qiskit/circuit/library/arithmetic/adders/__init__.py +18 -0
  45. qiskit/circuit/library/arithmetic/adders/adder.py +235 -0
  46. qiskit/circuit/library/arithmetic/adders/cdkm_ripple_carry_adder.py +123 -0
  47. qiskit/circuit/library/arithmetic/adders/draper_qft_adder.py +129 -0
  48. qiskit/circuit/library/arithmetic/adders/vbe_ripple_carry_adder.py +95 -0
  49. qiskit/circuit/library/arithmetic/exact_reciprocal.py +131 -0
  50. qiskit/circuit/library/arithmetic/functional_pauli_rotations.py +114 -0
  51. qiskit/circuit/library/arithmetic/integer_comparator.py +200 -0
  52. qiskit/circuit/library/arithmetic/linear_amplitude_function.py +363 -0
  53. qiskit/circuit/library/arithmetic/linear_pauli_rotations.py +243 -0
  54. qiskit/circuit/library/arithmetic/multipliers/__init__.py +17 -0
  55. qiskit/circuit/library/arithmetic/multipliers/hrs_cumulative_multiplier.py +145 -0
  56. qiskit/circuit/library/arithmetic/multipliers/multiplier.py +201 -0
  57. qiskit/circuit/library/arithmetic/multipliers/rg_qft_multiplier.py +108 -0
  58. qiskit/circuit/library/arithmetic/piecewise_chebyshev.py +502 -0
  59. qiskit/circuit/library/arithmetic/piecewise_linear_pauli_rotations.py +387 -0
  60. qiskit/circuit/library/arithmetic/piecewise_polynomial_pauli_rotations.py +493 -0
  61. qiskit/circuit/library/arithmetic/polynomial_pauli_rotations.py +389 -0
  62. qiskit/circuit/library/arithmetic/quadratic_form.py +364 -0
  63. qiskit/circuit/library/arithmetic/weighted_adder.py +409 -0
  64. qiskit/circuit/library/basis_change/__init__.py +15 -0
  65. qiskit/circuit/library/basis_change/qft.py +316 -0
  66. qiskit/circuit/library/bit_flip_oracle.py +130 -0
  67. qiskit/circuit/library/blueprintcircuit.py +316 -0
  68. qiskit/circuit/library/boolean_logic/__init__.py +18 -0
  69. qiskit/circuit/library/boolean_logic/inner_product.py +157 -0
  70. qiskit/circuit/library/boolean_logic/quantum_and.py +204 -0
  71. qiskit/circuit/library/boolean_logic/quantum_or.py +206 -0
  72. qiskit/circuit/library/boolean_logic/quantum_xor.py +167 -0
  73. qiskit/circuit/library/data_preparation/__init__.py +57 -0
  74. qiskit/circuit/library/data_preparation/_z_feature_map.py +115 -0
  75. qiskit/circuit/library/data_preparation/_zz_feature_map.py +150 -0
  76. qiskit/circuit/library/data_preparation/initializer.py +107 -0
  77. qiskit/circuit/library/data_preparation/pauli_feature_map.py +656 -0
  78. qiskit/circuit/library/data_preparation/state_preparation.py +336 -0
  79. qiskit/circuit/library/fourier_checking.py +160 -0
  80. qiskit/circuit/library/generalized_gates/__init__.py +30 -0
  81. qiskit/circuit/library/generalized_gates/diagonal.py +159 -0
  82. qiskit/circuit/library/generalized_gates/gms.py +175 -0
  83. qiskit/circuit/library/generalized_gates/gr.py +219 -0
  84. qiskit/circuit/library/generalized_gates/isometry.py +370 -0
  85. qiskit/circuit/library/generalized_gates/linear_function.py +318 -0
  86. qiskit/circuit/library/generalized_gates/mcg_up_to_diagonal.py +143 -0
  87. qiskit/circuit/library/generalized_gates/mcmt.py +316 -0
  88. qiskit/circuit/library/generalized_gates/pauli.py +84 -0
  89. qiskit/circuit/library/generalized_gates/permutation.py +198 -0
  90. qiskit/circuit/library/generalized_gates/rv.py +96 -0
  91. qiskit/circuit/library/generalized_gates/uc.py +303 -0
  92. qiskit/circuit/library/generalized_gates/uc_pauli_rot.py +164 -0
  93. qiskit/circuit/library/generalized_gates/ucrx.py +32 -0
  94. qiskit/circuit/library/generalized_gates/ucry.py +32 -0
  95. qiskit/circuit/library/generalized_gates/ucrz.py +32 -0
  96. qiskit/circuit/library/generalized_gates/unitary.py +217 -0
  97. qiskit/circuit/library/graph_state.py +172 -0
  98. qiskit/circuit/library/grover_operator.py +583 -0
  99. qiskit/circuit/library/hamiltonian_gate.py +142 -0
  100. qiskit/circuit/library/hidden_linear_function.py +163 -0
  101. qiskit/circuit/library/iqp.py +180 -0
  102. qiskit/circuit/library/n_local/__init__.py +45 -0
  103. qiskit/circuit/library/n_local/efficient_su2.py +282 -0
  104. qiskit/circuit/library/n_local/evolved_operator_ansatz.py +520 -0
  105. qiskit/circuit/library/n_local/excitation_preserving.py +303 -0
  106. qiskit/circuit/library/n_local/n_local.py +1477 -0
  107. qiskit/circuit/library/n_local/pauli_two_design.py +246 -0
  108. qiskit/circuit/library/n_local/qaoa_ansatz.py +367 -0
  109. qiskit/circuit/library/n_local/real_amplitudes.py +312 -0
  110. qiskit/circuit/library/n_local/two_local.py +289 -0
  111. qiskit/circuit/library/overlap.py +183 -0
  112. qiskit/circuit/library/pauli_evolution.py +201 -0
  113. qiskit/circuit/library/phase_estimation.py +177 -0
  114. qiskit/circuit/library/phase_oracle.py +239 -0
  115. qiskit/circuit/library/quantum_volume.py +180 -0
  116. qiskit/circuit/library/standard_gates/__init__.py +141 -0
  117. qiskit/circuit/library/standard_gates/dcx.py +77 -0
  118. qiskit/circuit/library/standard_gates/ecr.py +129 -0
  119. qiskit/circuit/library/standard_gates/equivalence_library.py +1800 -0
  120. qiskit/circuit/library/standard_gates/global_phase.py +84 -0
  121. qiskit/circuit/library/standard_gates/h.py +253 -0
  122. qiskit/circuit/library/standard_gates/i.py +76 -0
  123. qiskit/circuit/library/standard_gates/iswap.py +133 -0
  124. qiskit/circuit/library/standard_gates/p.py +422 -0
  125. qiskit/circuit/library/standard_gates/r.py +114 -0
  126. qiskit/circuit/library/standard_gates/rx.py +293 -0
  127. qiskit/circuit/library/standard_gates/rxx.py +180 -0
  128. qiskit/circuit/library/standard_gates/ry.py +286 -0
  129. qiskit/circuit/library/standard_gates/ryy.py +180 -0
  130. qiskit/circuit/library/standard_gates/rz.py +307 -0
  131. qiskit/circuit/library/standard_gates/rzx.py +226 -0
  132. qiskit/circuit/library/standard_gates/rzz.py +193 -0
  133. qiskit/circuit/library/standard_gates/s.py +419 -0
  134. qiskit/circuit/library/standard_gates/swap.py +281 -0
  135. qiskit/circuit/library/standard_gates/sx.py +310 -0
  136. qiskit/circuit/library/standard_gates/t.py +178 -0
  137. qiskit/circuit/library/standard_gates/u.py +395 -0
  138. qiskit/circuit/library/standard_gates/u1.py +490 -0
  139. qiskit/circuit/library/standard_gates/u2.py +145 -0
  140. qiskit/circuit/library/standard_gates/u3.py +428 -0
  141. qiskit/circuit/library/standard_gates/x.py +1481 -0
  142. qiskit/circuit/library/standard_gates/xx_minus_yy.py +202 -0
  143. qiskit/circuit/library/standard_gates/xx_plus_yy.py +236 -0
  144. qiskit/circuit/library/standard_gates/y.py +257 -0
  145. qiskit/circuit/library/standard_gates/z.py +338 -0
  146. qiskit/circuit/library/templates/__init__.py +92 -0
  147. qiskit/circuit/library/templates/clifford/__init__.py +33 -0
  148. qiskit/circuit/library/templates/clifford/clifford_2_1.py +34 -0
  149. qiskit/circuit/library/templates/clifford/clifford_2_2.py +35 -0
  150. qiskit/circuit/library/templates/clifford/clifford_2_3.py +34 -0
  151. qiskit/circuit/library/templates/clifford/clifford_2_4.py +34 -0
  152. qiskit/circuit/library/templates/clifford/clifford_3_1.py +35 -0
  153. qiskit/circuit/library/templates/clifford/clifford_4_1.py +38 -0
  154. qiskit/circuit/library/templates/clifford/clifford_4_2.py +37 -0
  155. qiskit/circuit/library/templates/clifford/clifford_4_3.py +38 -0
  156. qiskit/circuit/library/templates/clifford/clifford_4_4.py +37 -0
  157. qiskit/circuit/library/templates/clifford/clifford_5_1.py +40 -0
  158. qiskit/circuit/library/templates/clifford/clifford_6_1.py +40 -0
  159. qiskit/circuit/library/templates/clifford/clifford_6_2.py +40 -0
  160. qiskit/circuit/library/templates/clifford/clifford_6_3.py +40 -0
  161. qiskit/circuit/library/templates/clifford/clifford_6_4.py +38 -0
  162. qiskit/circuit/library/templates/clifford/clifford_6_5.py +40 -0
  163. qiskit/circuit/library/templates/clifford/clifford_8_1.py +42 -0
  164. qiskit/circuit/library/templates/clifford/clifford_8_2.py +42 -0
  165. qiskit/circuit/library/templates/clifford/clifford_8_3.py +41 -0
  166. qiskit/circuit/library/templates/nct/__init__.py +67 -0
  167. qiskit/circuit/library/templates/nct/template_nct_2a_1.py +34 -0
  168. qiskit/circuit/library/templates/nct/template_nct_2a_2.py +35 -0
  169. qiskit/circuit/library/templates/nct/template_nct_2a_3.py +37 -0
  170. qiskit/circuit/library/templates/nct/template_nct_4a_1.py +43 -0
  171. qiskit/circuit/library/templates/nct/template_nct_4a_2.py +41 -0
  172. qiskit/circuit/library/templates/nct/template_nct_4a_3.py +39 -0
  173. qiskit/circuit/library/templates/nct/template_nct_4b_1.py +41 -0
  174. qiskit/circuit/library/templates/nct/template_nct_4b_2.py +39 -0
  175. qiskit/circuit/library/templates/nct/template_nct_5a_1.py +40 -0
  176. qiskit/circuit/library/templates/nct/template_nct_5a_2.py +40 -0
  177. qiskit/circuit/library/templates/nct/template_nct_5a_3.py +40 -0
  178. qiskit/circuit/library/templates/nct/template_nct_5a_4.py +39 -0
  179. qiskit/circuit/library/templates/nct/template_nct_6a_1.py +40 -0
  180. qiskit/circuit/library/templates/nct/template_nct_6a_2.py +41 -0
  181. qiskit/circuit/library/templates/nct/template_nct_6a_3.py +41 -0
  182. qiskit/circuit/library/templates/nct/template_nct_6a_4.py +41 -0
  183. qiskit/circuit/library/templates/nct/template_nct_6b_1.py +41 -0
  184. qiskit/circuit/library/templates/nct/template_nct_6b_2.py +41 -0
  185. qiskit/circuit/library/templates/nct/template_nct_6c_1.py +41 -0
  186. qiskit/circuit/library/templates/nct/template_nct_7a_1.py +43 -0
  187. qiskit/circuit/library/templates/nct/template_nct_7b_1.py +43 -0
  188. qiskit/circuit/library/templates/nct/template_nct_7c_1.py +43 -0
  189. qiskit/circuit/library/templates/nct/template_nct_7d_1.py +43 -0
  190. qiskit/circuit/library/templates/nct/template_nct_7e_1.py +43 -0
  191. qiskit/circuit/library/templates/nct/template_nct_9a_1.py +45 -0
  192. qiskit/circuit/library/templates/nct/template_nct_9c_1.py +43 -0
  193. qiskit/circuit/library/templates/nct/template_nct_9c_10.py +44 -0
  194. qiskit/circuit/library/templates/nct/template_nct_9c_11.py +44 -0
  195. qiskit/circuit/library/templates/nct/template_nct_9c_12.py +44 -0
  196. qiskit/circuit/library/templates/nct/template_nct_9c_2.py +44 -0
  197. qiskit/circuit/library/templates/nct/template_nct_9c_3.py +44 -0
  198. qiskit/circuit/library/templates/nct/template_nct_9c_4.py +44 -0
  199. qiskit/circuit/library/templates/nct/template_nct_9c_5.py +44 -0
  200. qiskit/circuit/library/templates/nct/template_nct_9c_6.py +44 -0
  201. qiskit/circuit/library/templates/nct/template_nct_9c_7.py +44 -0
  202. qiskit/circuit/library/templates/nct/template_nct_9c_8.py +44 -0
  203. qiskit/circuit/library/templates/nct/template_nct_9c_9.py +44 -0
  204. qiskit/circuit/library/templates/nct/template_nct_9d_1.py +43 -0
  205. qiskit/circuit/library/templates/nct/template_nct_9d_10.py +44 -0
  206. qiskit/circuit/library/templates/nct/template_nct_9d_2.py +44 -0
  207. qiskit/circuit/library/templates/nct/template_nct_9d_3.py +44 -0
  208. qiskit/circuit/library/templates/nct/template_nct_9d_4.py +44 -0
  209. qiskit/circuit/library/templates/nct/template_nct_9d_5.py +44 -0
  210. qiskit/circuit/library/templates/nct/template_nct_9d_6.py +44 -0
  211. qiskit/circuit/library/templates/nct/template_nct_9d_7.py +44 -0
  212. qiskit/circuit/library/templates/nct/template_nct_9d_8.py +44 -0
  213. qiskit/circuit/library/templates/nct/template_nct_9d_9.py +44 -0
  214. qiskit/circuit/library/templates/rzx/__init__.py +25 -0
  215. qiskit/circuit/library/templates/rzx/rzx_cy.py +47 -0
  216. qiskit/circuit/library/templates/rzx/rzx_xz.py +54 -0
  217. qiskit/circuit/library/templates/rzx/rzx_yz.py +45 -0
  218. qiskit/circuit/library/templates/rzx/rzx_zz1.py +69 -0
  219. qiskit/circuit/library/templates/rzx/rzx_zz2.py +59 -0
  220. qiskit/circuit/library/templates/rzx/rzx_zz3.py +59 -0
  221. qiskit/circuit/measure.py +53 -0
  222. qiskit/circuit/operation.py +68 -0
  223. qiskit/circuit/parameter.py +179 -0
  224. qiskit/circuit/parameterexpression.py +703 -0
  225. qiskit/circuit/parametertable.py +119 -0
  226. qiskit/circuit/parametervector.py +140 -0
  227. qiskit/circuit/quantumcircuit.py +7540 -0
  228. qiskit/circuit/quantumcircuitdata.py +136 -0
  229. qiskit/circuit/random/__init__.py +15 -0
  230. qiskit/circuit/random/utils.py +366 -0
  231. qiskit/circuit/reset.py +37 -0
  232. qiskit/circuit/singleton.py +600 -0
  233. qiskit/circuit/store.py +89 -0
  234. qiskit/circuit/tools/__init__.py +16 -0
  235. qiskit/circuit/tools/pi_check.py +193 -0
  236. qiskit/circuit/twirling.py +145 -0
  237. qiskit/compiler/__init__.py +27 -0
  238. qiskit/compiler/transpiler.py +375 -0
  239. qiskit/converters/__init__.py +74 -0
  240. qiskit/converters/circuit_to_dag.py +80 -0
  241. qiskit/converters/circuit_to_dagdependency.py +49 -0
  242. qiskit/converters/circuit_to_dagdependency_v2.py +46 -0
  243. qiskit/converters/circuit_to_gate.py +107 -0
  244. qiskit/converters/circuit_to_instruction.py +142 -0
  245. qiskit/converters/dag_to_circuit.py +79 -0
  246. qiskit/converters/dag_to_dagdependency.py +54 -0
  247. qiskit/converters/dag_to_dagdependency_v2.py +43 -0
  248. qiskit/converters/dagdependency_to_circuit.py +40 -0
  249. qiskit/converters/dagdependency_to_dag.py +48 -0
  250. qiskit/dagcircuit/__init__.py +55 -0
  251. qiskit/dagcircuit/collect_blocks.py +407 -0
  252. qiskit/dagcircuit/dagcircuit.py +24 -0
  253. qiskit/dagcircuit/dagdependency.py +612 -0
  254. qiskit/dagcircuit/dagdependency_v2.py +566 -0
  255. qiskit/dagcircuit/dagdepnode.py +160 -0
  256. qiskit/dagcircuit/dagnode.py +188 -0
  257. qiskit/dagcircuit/exceptions.py +42 -0
  258. qiskit/exceptions.py +153 -0
  259. qiskit/passmanager/__init__.py +258 -0
  260. qiskit/passmanager/base_tasks.py +230 -0
  261. qiskit/passmanager/compilation_status.py +74 -0
  262. qiskit/passmanager/exceptions.py +19 -0
  263. qiskit/passmanager/flow_controllers.py +116 -0
  264. qiskit/passmanager/passmanager.py +353 -0
  265. qiskit/primitives/__init__.py +490 -0
  266. qiskit/primitives/backend_estimator_v2.py +530 -0
  267. qiskit/primitives/backend_sampler_v2.py +339 -0
  268. qiskit/primitives/base/__init__.py +20 -0
  269. qiskit/primitives/base/base_estimator.py +247 -0
  270. qiskit/primitives/base/base_primitive_job.py +78 -0
  271. qiskit/primitives/base/base_primitive_v1.py +45 -0
  272. qiskit/primitives/base/base_result_v1.py +65 -0
  273. qiskit/primitives/base/base_sampler.py +196 -0
  274. qiskit/primitives/base/estimator_result_v1.py +46 -0
  275. qiskit/primitives/base/sampler_result_v1.py +45 -0
  276. qiskit/primitives/base/validation_v1.py +250 -0
  277. qiskit/primitives/containers/__init__.py +26 -0
  278. qiskit/primitives/containers/bindings_array.py +391 -0
  279. qiskit/primitives/containers/bit_array.py +764 -0
  280. qiskit/primitives/containers/data_bin.py +175 -0
  281. qiskit/primitives/containers/estimator_pub.py +222 -0
  282. qiskit/primitives/containers/object_array.py +94 -0
  283. qiskit/primitives/containers/observables_array.py +296 -0
  284. qiskit/primitives/containers/primitive_result.py +53 -0
  285. qiskit/primitives/containers/pub_result.py +51 -0
  286. qiskit/primitives/containers/sampler_pub.py +193 -0
  287. qiskit/primitives/containers/sampler_pub_result.py +74 -0
  288. qiskit/primitives/containers/shape.py +129 -0
  289. qiskit/primitives/primitive_job.py +81 -0
  290. qiskit/primitives/statevector_estimator.py +175 -0
  291. qiskit/primitives/statevector_sampler.py +290 -0
  292. qiskit/primitives/utils.py +72 -0
  293. qiskit/providers/__init__.py +677 -0
  294. qiskit/providers/backend.py +364 -0
  295. qiskit/providers/basic_provider/__init__.py +47 -0
  296. qiskit/providers/basic_provider/basic_provider.py +121 -0
  297. qiskit/providers/basic_provider/basic_provider_job.py +65 -0
  298. qiskit/providers/basic_provider/basic_provider_tools.py +218 -0
  299. qiskit/providers/basic_provider/basic_simulator.py +693 -0
  300. qiskit/providers/basic_provider/exceptions.py +30 -0
  301. qiskit/providers/exceptions.py +33 -0
  302. qiskit/providers/fake_provider/__init__.py +69 -0
  303. qiskit/providers/fake_provider/generic_backend_v2.py +374 -0
  304. qiskit/providers/fake_provider/utils/__init__.py +15 -0
  305. qiskit/providers/job.py +147 -0
  306. qiskit/providers/jobstatus.py +30 -0
  307. qiskit/providers/options.py +273 -0
  308. qiskit/providers/providerutils.py +110 -0
  309. qiskit/qasm/libs/dummy/stdgates.inc +75 -0
  310. qiskit/qasm/libs/qelib1.inc +266 -0
  311. qiskit/qasm/libs/stdgates.inc +82 -0
  312. qiskit/qasm2/__init__.py +669 -0
  313. qiskit/qasm2/exceptions.py +27 -0
  314. qiskit/qasm2/export.py +364 -0
  315. qiskit/qasm2/parse.py +438 -0
  316. qiskit/qasm3/__init__.py +372 -0
  317. qiskit/qasm3/ast.py +782 -0
  318. qiskit/qasm3/exceptions.py +27 -0
  319. qiskit/qasm3/experimental.py +70 -0
  320. qiskit/qasm3/exporter.py +1340 -0
  321. qiskit/qasm3/printer.py +608 -0
  322. qiskit/qpy/__init__.py +1965 -0
  323. qiskit/qpy/binary_io/__init__.py +35 -0
  324. qiskit/qpy/binary_io/circuits.py +1455 -0
  325. qiskit/qpy/binary_io/parse_sympy_repr.py +121 -0
  326. qiskit/qpy/binary_io/schedules.py +308 -0
  327. qiskit/qpy/binary_io/value.py +1165 -0
  328. qiskit/qpy/common.py +353 -0
  329. qiskit/qpy/exceptions.py +53 -0
  330. qiskit/qpy/formats.py +442 -0
  331. qiskit/qpy/interface.py +344 -0
  332. qiskit/qpy/type_keys.py +409 -0
  333. qiskit/quantum_info/__init__.py +162 -0
  334. qiskit/quantum_info/analysis/__init__.py +17 -0
  335. qiskit/quantum_info/analysis/average.py +47 -0
  336. qiskit/quantum_info/analysis/distance.py +104 -0
  337. qiskit/quantum_info/analysis/make_observable.py +44 -0
  338. qiskit/quantum_info/analysis/z2_symmetries.py +484 -0
  339. qiskit/quantum_info/operators/__init__.py +28 -0
  340. qiskit/quantum_info/operators/base_operator.py +145 -0
  341. qiskit/quantum_info/operators/channel/__init__.py +29 -0
  342. qiskit/quantum_info/operators/channel/chi.py +191 -0
  343. qiskit/quantum_info/operators/channel/choi.py +218 -0
  344. qiskit/quantum_info/operators/channel/kraus.py +337 -0
  345. qiskit/quantum_info/operators/channel/ptm.py +204 -0
  346. qiskit/quantum_info/operators/channel/quantum_channel.py +348 -0
  347. qiskit/quantum_info/operators/channel/stinespring.py +296 -0
  348. qiskit/quantum_info/operators/channel/superop.py +373 -0
  349. qiskit/quantum_info/operators/channel/transformations.py +490 -0
  350. qiskit/quantum_info/operators/custom_iterator.py +48 -0
  351. qiskit/quantum_info/operators/dihedral/__init__.py +18 -0
  352. qiskit/quantum_info/operators/dihedral/dihedral.py +511 -0
  353. qiskit/quantum_info/operators/dihedral/dihedral_circuits.py +216 -0
  354. qiskit/quantum_info/operators/dihedral/polynomial.py +313 -0
  355. qiskit/quantum_info/operators/dihedral/random.py +64 -0
  356. qiskit/quantum_info/operators/linear_op.py +25 -0
  357. qiskit/quantum_info/operators/measures.py +418 -0
  358. qiskit/quantum_info/operators/mixins/__init__.py +52 -0
  359. qiskit/quantum_info/operators/mixins/adjoint.py +52 -0
  360. qiskit/quantum_info/operators/mixins/group.py +171 -0
  361. qiskit/quantum_info/operators/mixins/linear.py +84 -0
  362. qiskit/quantum_info/operators/mixins/multiply.py +62 -0
  363. qiskit/quantum_info/operators/mixins/tolerances.py +72 -0
  364. qiskit/quantum_info/operators/op_shape.py +525 -0
  365. qiskit/quantum_info/operators/operator.py +869 -0
  366. qiskit/quantum_info/operators/operator_utils.py +76 -0
  367. qiskit/quantum_info/operators/predicates.py +183 -0
  368. qiskit/quantum_info/operators/random.py +154 -0
  369. qiskit/quantum_info/operators/scalar_op.py +254 -0
  370. qiskit/quantum_info/operators/symplectic/__init__.py +23 -0
  371. qiskit/quantum_info/operators/symplectic/base_pauli.py +719 -0
  372. qiskit/quantum_info/operators/symplectic/clifford.py +1032 -0
  373. qiskit/quantum_info/operators/symplectic/clifford_circuits.py +558 -0
  374. qiskit/quantum_info/operators/symplectic/pauli.py +755 -0
  375. qiskit/quantum_info/operators/symplectic/pauli_list.py +1242 -0
  376. qiskit/quantum_info/operators/symplectic/pauli_utils.py +40 -0
  377. qiskit/quantum_info/operators/symplectic/random.py +117 -0
  378. qiskit/quantum_info/operators/symplectic/sparse_pauli_op.py +1239 -0
  379. qiskit/quantum_info/operators/utils/__init__.py +20 -0
  380. qiskit/quantum_info/operators/utils/anti_commutator.py +36 -0
  381. qiskit/quantum_info/operators/utils/commutator.py +36 -0
  382. qiskit/quantum_info/operators/utils/double_commutator.py +76 -0
  383. qiskit/quantum_info/quaternion.py +156 -0
  384. qiskit/quantum_info/random.py +26 -0
  385. qiskit/quantum_info/states/__init__.py +28 -0
  386. qiskit/quantum_info/states/densitymatrix.py +857 -0
  387. qiskit/quantum_info/states/measures.py +288 -0
  388. qiskit/quantum_info/states/quantum_state.py +503 -0
  389. qiskit/quantum_info/states/random.py +157 -0
  390. qiskit/quantum_info/states/stabilizerstate.py +805 -0
  391. qiskit/quantum_info/states/statevector.py +977 -0
  392. qiskit/quantum_info/states/utils.py +247 -0
  393. qiskit/result/__init__.py +61 -0
  394. qiskit/result/counts.py +189 -0
  395. qiskit/result/distributions/__init__.py +17 -0
  396. qiskit/result/distributions/probability.py +100 -0
  397. qiskit/result/distributions/quasi.py +154 -0
  398. qiskit/result/exceptions.py +40 -0
  399. qiskit/result/models.py +241 -0
  400. qiskit/result/postprocess.py +239 -0
  401. qiskit/result/result.py +385 -0
  402. qiskit/result/sampled_expval.py +74 -0
  403. qiskit/result/utils.py +294 -0
  404. qiskit/synthesis/__init__.py +240 -0
  405. qiskit/synthesis/arithmetic/__init__.py +18 -0
  406. qiskit/synthesis/arithmetic/adders/__init__.py +17 -0
  407. qiskit/synthesis/arithmetic/adders/cdkm_ripple_carry_adder.py +154 -0
  408. qiskit/synthesis/arithmetic/adders/draper_qft_adder.py +103 -0
  409. qiskit/synthesis/arithmetic/adders/vbe_ripple_carry_adder.py +161 -0
  410. qiskit/synthesis/arithmetic/comparators/__init__.py +16 -0
  411. qiskit/synthesis/arithmetic/comparators/compare_2s.py +112 -0
  412. qiskit/synthesis/arithmetic/comparators/compare_greedy.py +66 -0
  413. qiskit/synthesis/arithmetic/multipliers/__init__.py +16 -0
  414. qiskit/synthesis/arithmetic/multipliers/hrs_cumulative_multiplier.py +103 -0
  415. qiskit/synthesis/arithmetic/multipliers/rg_qft_multiplier.py +100 -0
  416. qiskit/synthesis/arithmetic/weighted_sum.py +155 -0
  417. qiskit/synthesis/boolean/__init__.py +13 -0
  418. qiskit/synthesis/boolean/boolean_expression.py +231 -0
  419. qiskit/synthesis/boolean/boolean_expression_synth.py +124 -0
  420. qiskit/synthesis/boolean/boolean_expression_visitor.py +96 -0
  421. qiskit/synthesis/clifford/__init__.py +19 -0
  422. qiskit/synthesis/clifford/clifford_decompose_ag.py +178 -0
  423. qiskit/synthesis/clifford/clifford_decompose_bm.py +46 -0
  424. qiskit/synthesis/clifford/clifford_decompose_full.py +64 -0
  425. qiskit/synthesis/clifford/clifford_decompose_greedy.py +58 -0
  426. qiskit/synthesis/clifford/clifford_decompose_layers.py +447 -0
  427. qiskit/synthesis/cnotdihedral/__init__.py +17 -0
  428. qiskit/synthesis/cnotdihedral/cnotdihedral_decompose_full.py +52 -0
  429. qiskit/synthesis/cnotdihedral/cnotdihedral_decompose_general.py +141 -0
  430. qiskit/synthesis/cnotdihedral/cnotdihedral_decompose_two_qubits.py +266 -0
  431. qiskit/synthesis/discrete_basis/__init__.py +16 -0
  432. qiskit/synthesis/discrete_basis/commutator_decompose.py +265 -0
  433. qiskit/synthesis/discrete_basis/gate_sequence.py +421 -0
  434. qiskit/synthesis/discrete_basis/generate_basis_approximations.py +165 -0
  435. qiskit/synthesis/discrete_basis/solovay_kitaev.py +240 -0
  436. qiskit/synthesis/evolution/__init__.py +21 -0
  437. qiskit/synthesis/evolution/evolution_synthesis.py +48 -0
  438. qiskit/synthesis/evolution/lie_trotter.py +120 -0
  439. qiskit/synthesis/evolution/matrix_synthesis.py +47 -0
  440. qiskit/synthesis/evolution/pauli_network.py +80 -0
  441. qiskit/synthesis/evolution/product_formula.py +313 -0
  442. qiskit/synthesis/evolution/qdrift.py +130 -0
  443. qiskit/synthesis/evolution/suzuki_trotter.py +224 -0
  444. qiskit/synthesis/linear/__init__.py +26 -0
  445. qiskit/synthesis/linear/cnot_synth.py +69 -0
  446. qiskit/synthesis/linear/linear_circuits_utils.py +128 -0
  447. qiskit/synthesis/linear/linear_depth_lnn.py +61 -0
  448. qiskit/synthesis/linear/linear_matrix_utils.py +27 -0
  449. qiskit/synthesis/linear_phase/__init__.py +17 -0
  450. qiskit/synthesis/linear_phase/cnot_phase_synth.py +206 -0
  451. qiskit/synthesis/linear_phase/cx_cz_depth_lnn.py +61 -0
  452. qiskit/synthesis/linear_phase/cz_depth_lnn.py +58 -0
  453. qiskit/synthesis/multi_controlled/__init__.py +25 -0
  454. qiskit/synthesis/multi_controlled/mcmt_vchain.py +52 -0
  455. qiskit/synthesis/multi_controlled/mcx_synthesis.py +359 -0
  456. qiskit/synthesis/multi_controlled/multi_control_rotation_gates.py +206 -0
  457. qiskit/synthesis/one_qubit/__init__.py +15 -0
  458. qiskit/synthesis/one_qubit/one_qubit_decompose.py +288 -0
  459. qiskit/synthesis/permutation/__init__.py +18 -0
  460. qiskit/synthesis/permutation/permutation_full.py +78 -0
  461. qiskit/synthesis/permutation/permutation_lnn.py +54 -0
  462. qiskit/synthesis/permutation/permutation_reverse_lnn.py +93 -0
  463. qiskit/synthesis/permutation/permutation_utils.py +16 -0
  464. qiskit/synthesis/qft/__init__.py +16 -0
  465. qiskit/synthesis/qft/qft_decompose_full.py +97 -0
  466. qiskit/synthesis/qft/qft_decompose_lnn.py +79 -0
  467. qiskit/synthesis/stabilizer/__init__.py +16 -0
  468. qiskit/synthesis/stabilizer/stabilizer_circuit.py +149 -0
  469. qiskit/synthesis/stabilizer/stabilizer_decompose.py +194 -0
  470. qiskit/synthesis/two_qubit/__init__.py +20 -0
  471. qiskit/synthesis/two_qubit/local_invariance.py +63 -0
  472. qiskit/synthesis/two_qubit/two_qubit_decompose.py +583 -0
  473. qiskit/synthesis/two_qubit/xx_decompose/__init__.py +19 -0
  474. qiskit/synthesis/two_qubit/xx_decompose/circuits.py +300 -0
  475. qiskit/synthesis/two_qubit/xx_decompose/decomposer.py +324 -0
  476. qiskit/synthesis/two_qubit/xx_decompose/embodiments.py +163 -0
  477. qiskit/synthesis/two_qubit/xx_decompose/paths.py +412 -0
  478. qiskit/synthesis/two_qubit/xx_decompose/polytopes.py +262 -0
  479. qiskit/synthesis/two_qubit/xx_decompose/utilities.py +40 -0
  480. qiskit/synthesis/two_qubit/xx_decompose/weyl.py +133 -0
  481. qiskit/synthesis/unitary/__init__.py +13 -0
  482. qiskit/synthesis/unitary/aqc/__init__.py +177 -0
  483. qiskit/synthesis/unitary/aqc/approximate.py +116 -0
  484. qiskit/synthesis/unitary/aqc/aqc.py +175 -0
  485. qiskit/synthesis/unitary/aqc/cnot_structures.py +300 -0
  486. qiskit/synthesis/unitary/aqc/cnot_unit_circuit.py +103 -0
  487. qiskit/synthesis/unitary/aqc/cnot_unit_objective.py +299 -0
  488. qiskit/synthesis/unitary/aqc/elementary_operations.py +108 -0
  489. qiskit/synthesis/unitary/aqc/fast_gradient/__init__.py +164 -0
  490. qiskit/synthesis/unitary/aqc/fast_gradient/fast_grad_utils.py +237 -0
  491. qiskit/synthesis/unitary/aqc/fast_gradient/fast_gradient.py +226 -0
  492. qiskit/synthesis/unitary/aqc/fast_gradient/layer.py +370 -0
  493. qiskit/synthesis/unitary/aqc/fast_gradient/pmatrix.py +312 -0
  494. qiskit/synthesis/unitary/qsd.py +288 -0
  495. qiskit/transpiler/__init__.py +1345 -0
  496. qiskit/transpiler/basepasses.py +190 -0
  497. qiskit/transpiler/coupling.py +500 -0
  498. qiskit/transpiler/exceptions.py +59 -0
  499. qiskit/transpiler/instruction_durations.py +281 -0
  500. qiskit/transpiler/layout.py +740 -0
  501. qiskit/transpiler/passes/__init__.py +276 -0
  502. qiskit/transpiler/passes/analysis/__init__.py +23 -0
  503. qiskit/transpiler/passes/analysis/count_ops.py +30 -0
  504. qiskit/transpiler/passes/analysis/count_ops_longest_path.py +26 -0
  505. qiskit/transpiler/passes/analysis/dag_longest_path.py +24 -0
  506. qiskit/transpiler/passes/analysis/depth.py +33 -0
  507. qiskit/transpiler/passes/analysis/num_qubits.py +26 -0
  508. qiskit/transpiler/passes/analysis/num_tensor_factors.py +26 -0
  509. qiskit/transpiler/passes/analysis/resource_estimation.py +41 -0
  510. qiskit/transpiler/passes/analysis/size.py +36 -0
  511. qiskit/transpiler/passes/analysis/width.py +27 -0
  512. qiskit/transpiler/passes/basis/__init__.py +19 -0
  513. qiskit/transpiler/passes/basis/basis_translator.py +138 -0
  514. qiskit/transpiler/passes/basis/decompose.py +137 -0
  515. qiskit/transpiler/passes/basis/translate_parameterized.py +175 -0
  516. qiskit/transpiler/passes/basis/unroll_3q_or_more.py +84 -0
  517. qiskit/transpiler/passes/basis/unroll_custom_definitions.py +110 -0
  518. qiskit/transpiler/passes/layout/__init__.py +26 -0
  519. qiskit/transpiler/passes/layout/_csp_custom_solver.py +65 -0
  520. qiskit/transpiler/passes/layout/apply_layout.py +128 -0
  521. qiskit/transpiler/passes/layout/csp_layout.py +132 -0
  522. qiskit/transpiler/passes/layout/dense_layout.py +177 -0
  523. qiskit/transpiler/passes/layout/disjoint_utils.py +219 -0
  524. qiskit/transpiler/passes/layout/enlarge_with_ancilla.py +49 -0
  525. qiskit/transpiler/passes/layout/full_ancilla_allocation.py +116 -0
  526. qiskit/transpiler/passes/layout/layout_2q_distance.py +77 -0
  527. qiskit/transpiler/passes/layout/sabre_layout.py +506 -0
  528. qiskit/transpiler/passes/layout/sabre_pre_layout.py +225 -0
  529. qiskit/transpiler/passes/layout/set_layout.py +69 -0
  530. qiskit/transpiler/passes/layout/trivial_layout.py +66 -0
  531. qiskit/transpiler/passes/layout/vf2_layout.py +256 -0
  532. qiskit/transpiler/passes/layout/vf2_post_layout.py +376 -0
  533. qiskit/transpiler/passes/layout/vf2_utils.py +235 -0
  534. qiskit/transpiler/passes/optimization/__init__.py +42 -0
  535. qiskit/transpiler/passes/optimization/_gate_extension.py +80 -0
  536. qiskit/transpiler/passes/optimization/collect_1q_runs.py +31 -0
  537. qiskit/transpiler/passes/optimization/collect_2q_blocks.py +35 -0
  538. qiskit/transpiler/passes/optimization/collect_and_collapse.py +117 -0
  539. qiskit/transpiler/passes/optimization/collect_cliffords.py +109 -0
  540. qiskit/transpiler/passes/optimization/collect_linear_functions.py +85 -0
  541. qiskit/transpiler/passes/optimization/collect_multiqubit_blocks.py +242 -0
  542. qiskit/transpiler/passes/optimization/commutation_analysis.py +44 -0
  543. qiskit/transpiler/passes/optimization/commutative_cancellation.py +82 -0
  544. qiskit/transpiler/passes/optimization/commutative_inverse_cancellation.py +140 -0
  545. qiskit/transpiler/passes/optimization/consolidate_blocks.py +176 -0
  546. qiskit/transpiler/passes/optimization/contract_idle_wires_in_control_flow.py +104 -0
  547. qiskit/transpiler/passes/optimization/elide_permutations.py +91 -0
  548. qiskit/transpiler/passes/optimization/hoare_opt.py +420 -0
  549. qiskit/transpiler/passes/optimization/inverse_cancellation.py +95 -0
  550. qiskit/transpiler/passes/optimization/light_cone.py +135 -0
  551. qiskit/transpiler/passes/optimization/optimize_1q_commutation.py +267 -0
  552. qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py +250 -0
  553. qiskit/transpiler/passes/optimization/optimize_1q_gates.py +384 -0
  554. qiskit/transpiler/passes/optimization/optimize_annotated.py +449 -0
  555. qiskit/transpiler/passes/optimization/optimize_cliffords.py +89 -0
  556. qiskit/transpiler/passes/optimization/optimize_swap_before_measure.py +71 -0
  557. qiskit/transpiler/passes/optimization/remove_diagonal_gates_before_measure.py +41 -0
  558. qiskit/transpiler/passes/optimization/remove_final_reset.py +37 -0
  559. qiskit/transpiler/passes/optimization/remove_identity_equiv.py +70 -0
  560. qiskit/transpiler/passes/optimization/remove_reset_in_zero_state.py +37 -0
  561. qiskit/transpiler/passes/optimization/reset_after_measure_simplification.py +50 -0
  562. qiskit/transpiler/passes/optimization/split_2q_unitaries.py +63 -0
  563. qiskit/transpiler/passes/optimization/template_matching/__init__.py +19 -0
  564. qiskit/transpiler/passes/optimization/template_matching/backward_match.py +749 -0
  565. qiskit/transpiler/passes/optimization/template_matching/forward_match.py +452 -0
  566. qiskit/transpiler/passes/optimization/template_matching/maximal_matches.py +77 -0
  567. qiskit/transpiler/passes/optimization/template_matching/template_matching.py +370 -0
  568. qiskit/transpiler/passes/optimization/template_matching/template_substitution.py +639 -0
  569. qiskit/transpiler/passes/optimization/template_optimization.py +158 -0
  570. qiskit/transpiler/passes/routing/__init__.py +21 -0
  571. qiskit/transpiler/passes/routing/algorithms/__init__.py +33 -0
  572. qiskit/transpiler/passes/routing/algorithms/token_swapper.py +105 -0
  573. qiskit/transpiler/passes/routing/algorithms/types.py +46 -0
  574. qiskit/transpiler/passes/routing/algorithms/util.py +103 -0
  575. qiskit/transpiler/passes/routing/basic_swap.py +166 -0
  576. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/__init__.py +25 -0
  577. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_block.py +60 -0
  578. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/commuting_2q_gate_router.py +397 -0
  579. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/pauli_2q_evolution_commutation.py +145 -0
  580. qiskit/transpiler/passes/routing/commuting_2q_gate_routing/swap_strategy.py +306 -0
  581. qiskit/transpiler/passes/routing/layout_transformation.py +119 -0
  582. qiskit/transpiler/passes/routing/lookahead_swap.py +390 -0
  583. qiskit/transpiler/passes/routing/sabre_swap.py +463 -0
  584. qiskit/transpiler/passes/routing/star_prerouting.py +408 -0
  585. qiskit/transpiler/passes/routing/utils.py +35 -0
  586. qiskit/transpiler/passes/scheduling/__init__.py +21 -0
  587. qiskit/transpiler/passes/scheduling/alignments/__init__.py +79 -0
  588. qiskit/transpiler/passes/scheduling/alignments/check_durations.py +70 -0
  589. qiskit/transpiler/passes/scheduling/alignments/reschedule.py +251 -0
  590. qiskit/transpiler/passes/scheduling/padding/__init__.py +16 -0
  591. qiskit/transpiler/passes/scheduling/padding/base_padding.py +284 -0
  592. qiskit/transpiler/passes/scheduling/padding/dynamical_decoupling.py +415 -0
  593. qiskit/transpiler/passes/scheduling/padding/pad_delay.py +90 -0
  594. qiskit/transpiler/passes/scheduling/scheduling/__init__.py +17 -0
  595. qiskit/transpiler/passes/scheduling/scheduling/alap.py +93 -0
  596. qiskit/transpiler/passes/scheduling/scheduling/asap.py +100 -0
  597. qiskit/transpiler/passes/scheduling/scheduling/base_scheduler.py +88 -0
  598. qiskit/transpiler/passes/scheduling/scheduling/set_io_latency.py +64 -0
  599. qiskit/transpiler/passes/scheduling/time_unit_conversion.py +237 -0
  600. qiskit/transpiler/passes/synthesis/__init__.py +20 -0
  601. qiskit/transpiler/passes/synthesis/aqc_plugin.py +153 -0
  602. qiskit/transpiler/passes/synthesis/default_unitary_synth_plugin.py +653 -0
  603. qiskit/transpiler/passes/synthesis/high_level_synthesis.py +429 -0
  604. qiskit/transpiler/passes/synthesis/hls_plugins.py +1963 -0
  605. qiskit/transpiler/passes/synthesis/linear_functions_synthesis.py +41 -0
  606. qiskit/transpiler/passes/synthesis/plugin.py +738 -0
  607. qiskit/transpiler/passes/synthesis/solovay_kitaev_synthesis.py +313 -0
  608. qiskit/transpiler/passes/synthesis/unitary_synthesis.py +425 -0
  609. qiskit/transpiler/passes/utils/__init__.py +32 -0
  610. qiskit/transpiler/passes/utils/barrier_before_final_measurements.py +41 -0
  611. qiskit/transpiler/passes/utils/check_gate_direction.py +60 -0
  612. qiskit/transpiler/passes/utils/check_map.py +78 -0
  613. qiskit/transpiler/passes/utils/contains_instruction.py +45 -0
  614. qiskit/transpiler/passes/utils/control_flow.py +61 -0
  615. qiskit/transpiler/passes/utils/dag_fixed_point.py +36 -0
  616. qiskit/transpiler/passes/utils/error.py +69 -0
  617. qiskit/transpiler/passes/utils/filter_op_nodes.py +66 -0
  618. qiskit/transpiler/passes/utils/fixed_point.py +48 -0
  619. qiskit/transpiler/passes/utils/gate_direction.py +93 -0
  620. qiskit/transpiler/passes/utils/gates_basis.py +51 -0
  621. qiskit/transpiler/passes/utils/merge_adjacent_barriers.py +163 -0
  622. qiskit/transpiler/passes/utils/minimum_point.py +118 -0
  623. qiskit/transpiler/passes/utils/remove_barriers.py +50 -0
  624. qiskit/transpiler/passes/utils/remove_final_measurements.py +121 -0
  625. qiskit/transpiler/passes/utils/unroll_forloops.py +81 -0
  626. qiskit/transpiler/passmanager.py +503 -0
  627. qiskit/transpiler/passmanager_config.py +151 -0
  628. qiskit/transpiler/preset_passmanagers/__init__.py +93 -0
  629. qiskit/transpiler/preset_passmanagers/builtin_plugins.py +993 -0
  630. qiskit/transpiler/preset_passmanagers/common.py +672 -0
  631. qiskit/transpiler/preset_passmanagers/generate_preset_pass_manager.py +437 -0
  632. qiskit/transpiler/preset_passmanagers/level0.py +104 -0
  633. qiskit/transpiler/preset_passmanagers/level1.py +108 -0
  634. qiskit/transpiler/preset_passmanagers/level2.py +109 -0
  635. qiskit/transpiler/preset_passmanagers/level3.py +110 -0
  636. qiskit/transpiler/preset_passmanagers/plugin.py +346 -0
  637. qiskit/transpiler/target.py +905 -0
  638. qiskit/transpiler/timing_constraints.py +59 -0
  639. qiskit/user_config.py +266 -0
  640. qiskit/utils/__init__.py +90 -0
  641. qiskit/utils/classtools.py +146 -0
  642. qiskit/utils/deprecation.py +382 -0
  643. qiskit/utils/lazy_tester.py +363 -0
  644. qiskit/utils/optionals.py +354 -0
  645. qiskit/utils/parallel.py +318 -0
  646. qiskit/utils/units.py +146 -0
  647. qiskit/version.py +84 -0
  648. qiskit/visualization/__init__.py +290 -0
  649. qiskit/visualization/array.py +207 -0
  650. qiskit/visualization/bloch.py +778 -0
  651. qiskit/visualization/circuit/__init__.py +15 -0
  652. qiskit/visualization/circuit/_utils.py +675 -0
  653. qiskit/visualization/circuit/circuit_visualization.py +735 -0
  654. qiskit/visualization/circuit/latex.py +661 -0
  655. qiskit/visualization/circuit/matplotlib.py +2019 -0
  656. qiskit/visualization/circuit/qcstyle.py +278 -0
  657. qiskit/visualization/circuit/styles/__init__.py +13 -0
  658. qiskit/visualization/circuit/styles/bw.json +202 -0
  659. qiskit/visualization/circuit/styles/clifford.json +202 -0
  660. qiskit/visualization/circuit/styles/iqp-dark.json +214 -0
  661. qiskit/visualization/circuit/styles/iqp.json +214 -0
  662. qiskit/visualization/circuit/styles/textbook.json +202 -0
  663. qiskit/visualization/circuit/text.py +1849 -0
  664. qiskit/visualization/circuit_visualization.py +19 -0
  665. qiskit/visualization/counts_visualization.py +487 -0
  666. qiskit/visualization/dag_visualization.py +318 -0
  667. qiskit/visualization/exceptions.py +21 -0
  668. qiskit/visualization/gate_map.py +1424 -0
  669. qiskit/visualization/library.py +40 -0
  670. qiskit/visualization/pass_manager_visualization.py +312 -0
  671. qiskit/visualization/state_visualization.py +1546 -0
  672. qiskit/visualization/timeline/__init__.py +21 -0
  673. qiskit/visualization/timeline/core.py +495 -0
  674. qiskit/visualization/timeline/drawings.py +260 -0
  675. qiskit/visualization/timeline/generators.py +506 -0
  676. qiskit/visualization/timeline/interface.py +444 -0
  677. qiskit/visualization/timeline/layouts.py +115 -0
  678. qiskit/visualization/timeline/plotters/__init__.py +16 -0
  679. qiskit/visualization/timeline/plotters/base_plotter.py +58 -0
  680. qiskit/visualization/timeline/plotters/matplotlib.py +195 -0
  681. qiskit/visualization/timeline/stylesheet.py +301 -0
  682. qiskit/visualization/timeline/types.py +148 -0
  683. qiskit/visualization/transition_visualization.py +369 -0
  684. qiskit/visualization/utils.py +49 -0
  685. qiskit-2.0.3.dist-info/METADATA +220 -0
  686. qiskit-2.0.3.dist-info/RECORD +690 -0
  687. qiskit-2.0.3.dist-info/WHEEL +6 -0
  688. qiskit-2.0.3.dist-info/entry_points.txt +82 -0
  689. qiskit-2.0.3.dist-info/licenses/LICENSE.txt +203 -0
  690. qiskit-2.0.3.dist-info/top_level.txt +1 -0
qiskit/qpy/__init__.py ADDED
@@ -0,0 +1,1965 @@
1
+ # This code is part of Qiskit.
2
+ #
3
+ # (C) Copyright IBM 2021, 2022.
4
+ #
5
+ # This code is licensed under the Apache License, Version 2.0. You may
6
+ # obtain a copy of this license in the LICENSE.txt file in the root directory
7
+ # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8
+ #
9
+ # Any modifications or derivative works of this code must retain this
10
+ # copyright notice, and modified files need to carry a notice indicating
11
+ # that they have been altered from the originals.
12
+
13
+ """
14
+ =====================================
15
+ QPY serialization (:mod:`qiskit.qpy`)
16
+ =====================================
17
+
18
+ .. currentmodule:: qiskit.qpy
19
+
20
+ QPY is a binary serialization format for :class:`~.QuantumCircuit`
21
+ objects that is designed to be cross-platform, Python version agnostic,
22
+ and backwards compatible moving forward. QPY should be used if you need
23
+ a mechanism to save or copy between systems a :class:`~.QuantumCircuit`
24
+ that preserves the full Qiskit object structure (except for custom attributes
25
+ defined outside of Qiskit code). This differs from other serialization formats like
26
+ `OpenQASM <https://github.com/openqasm/openqasm>`__ (2.0 or 3.0) which has a
27
+ different abstraction model and can result in a loss of information contained
28
+ in the original circuit (or is unable to represent some aspects of the
29
+ Qiskit objects) or Python's `pickle <https://docs.python.org/3/library/pickle.html>`__
30
+ which will preserve the Qiskit object exactly but will only work for a single Qiskit
31
+ version (it is also
32
+ `potentially insecure <https://docs.python.org/3/library/pickle.html#module-pickle>`__).
33
+
34
+ Basic Usage
35
+ ===========
36
+
37
+ Using QPY is defined to be straightforward and mirror the user API of the
38
+ serializers in Python's standard library, ``pickle`` and ``json``. There are
39
+ 2 user facing functions: :func:`qiskit.qpy.dump` and
40
+ :func:`qiskit.qpy.load` which are used to dump QPY data
41
+ to a file object and load circuits from QPY data in a file object respectively.
42
+ For example:
43
+
44
+ .. plot::
45
+ :nofigs:
46
+ :context: reset
47
+
48
+ # This code is hidden from users
49
+ # It's a hack to avoid writing to file when testing the code examples
50
+ import io
51
+ bytestream = io.BytesIO()
52
+ bytestream.close = lambda: bytestream.seek(0)
53
+ def open(*args):
54
+ return bytestream
55
+
56
+ .. plot::
57
+ :include-source:
58
+ :nofigs:
59
+ :context:
60
+
61
+ from qiskit.circuit import QuantumCircuit
62
+ from qiskit import qpy
63
+
64
+ qc = QuantumCircuit(2, name='Bell', metadata={'test': True})
65
+ qc.h(0)
66
+ qc.cx(0, 1)
67
+ qc.measure_all()
68
+
69
+ with open('bell.qpy', 'wb') as fd:
70
+ qpy.dump(qc, fd)
71
+
72
+ with open('bell.qpy', 'rb') as fd:
73
+ new_qc = qpy.load(fd)[0]
74
+
75
+ The :func:`qiskit.qpy.dump` function also lets you
76
+ include multiple circuits in a single QPY file:
77
+
78
+ .. plot::
79
+ :include-source:
80
+ :nofigs:
81
+ :context:
82
+
83
+ with open('twenty_bells.qpy', 'wb') as fd:
84
+ qpy.dump([qc] * 20, fd)
85
+
86
+ and then loading that file will return a list with all the circuits
87
+
88
+ .. plot::
89
+ :include-source:
90
+ :nofigs:
91
+ :context:
92
+
93
+ with open('twenty_bells.qpy', 'rb') as fd:
94
+ twenty_new_bells = qpy.load(fd)
95
+
96
+
97
+ API documentation
98
+ =================
99
+
100
+ .. autofunction:: load
101
+ .. autofunction:: dump
102
+
103
+ These functions will raise a custom subclass of :exc:`.QiskitError` if they encounter problems
104
+ during serialization or deserialization.
105
+
106
+ .. autoexception:: QpyError
107
+
108
+ When a lower-than-maximum target QPY version is set for serialization, but the object to be
109
+ serialized contains features that cannot be represented in that format, a subclass of
110
+ :exc:`QpyError` is raised:
111
+
112
+ .. autoexception:: UnsupportedFeatureForVersion
113
+
114
+ Attributes:
115
+ QPY_VERSION (int): The current QPY format version as of this release. This
116
+ is the default value of the ``version`` keyword argument on
117
+ :func:`.qpy.dump` and also the upper bound for accepted values for
118
+ the same argument. This is also the upper bond on the versions supported
119
+ by :func:`.qpy.load`.
120
+
121
+ QPY_COMPATIBILITY_VERSION (int): The current minimum compatibility QPY
122
+ format version. This is the minimum version that :func:`.qpy.dump`
123
+ will accept for the ``version`` keyword argument. :func:`.qpy.load`
124
+ will be able to load all released format versions of QPY (up until
125
+ ``QPY_VERSION``).
126
+
127
+ .. _qpy_compatibility:
128
+
129
+ QPY Compatibility
130
+ =================
131
+
132
+ The QPY format is designed to be backwards compatible moving forward. This means
133
+ you should be able to load a QPY with any newer Qiskit version than the one
134
+ that generated it. However, loading a QPY file with an older Qiskit version is
135
+ not supported and may not work.
136
+
137
+ For example, if you generated a QPY file using qiskit-terra 0.18.1 you could
138
+ load that QPY file with qiskit-terra 0.19.0 and a hypothetical qiskit-terra
139
+ 0.29.0. However, loading that QPY file with 0.18.0 is not supported and may not
140
+ work.
141
+
142
+ If a feature being loaded is deprecated in the corresponding qiskit release, QPY will
143
+ raise a :exc:`~.QPYLoadingDeprecatedFeatureWarning` informing of the deprecation period
144
+ and how the feature will be internally handled.
145
+
146
+ .. autoexception:: QPYLoadingDeprecatedFeatureWarning
147
+
148
+ .. note::
149
+
150
+ With versions of Qiskit before 1.2.4, the ``use_symengine=True`` argument to :func:`.qpy.dump`
151
+ could cause problems with backwards compatibility if there were :class:`.ParameterExpression`
152
+ objects to serialize. In particular:
153
+
154
+ * When the loading version of Qiskit is 1.2.4 or greater, QPY files generated with any version
155
+ of Qiskit >= 0.46.0 can be loaded. If a version of Qiskit between 0.45.0 and 0.45.3 was used
156
+ to generate the files, and the non-default argument ``use_symengine=True`` was given to
157
+ :func:`.qpy.dump`, the file can only be read if the version of ``symengine`` used in the
158
+ generating environment was in the 0.11 or 0.13 series, but if the environment was created
159
+ during the support window of Qiskit 0.45, it is likely that ``symengine==0.9.2`` was used.
160
+
161
+ * When the loading version of Qiskit is between 0.46.0 and 1.2.2 inclusive, the file can only be
162
+ read if the installed version of ``symengine`` in the loading environment matches the version
163
+ used in the generating environment.
164
+
165
+ To recover a QPY file that fails with ``symengine`` version-related errors during a call to
166
+ :func:`.qpy.load`, first attempt to use Qiskit >= 1.2.4 to load the file. If this still fails,
167
+ it is likely because Qiskit 0.45.x was used to generate the file with ``use_symengine=True``.
168
+ In this case, use Qiskit 0.45.3 with ``symengine==0.9.2`` to load the file, and then re-export
169
+ it to QPY setting ``use_symengine=False``. The resulting file can then be loaded by any later
170
+ version of Qiskit.
171
+
172
+ .. note::
173
+
174
+ Starting with Qiskit version 2.0.0, which removed the Pulse module from the library, QPY provides
175
+ limited support for loading payloads that include pulse data. Loading a ``ScheduleBlock`` payload,
176
+ a :class:`.QpyError` exception will be raised. Loading a payload for a circuit that contained pulse
177
+ gates, the output circuit will contain custom instructions **without** calibration data attached
178
+ for each pulse gate, leaving them undefined.
179
+
180
+ QPY format version history
181
+ --------------------------
182
+
183
+ If you're planning to load a QPY file between different Qiskit versions knowing
184
+ which versions were available in a given release are useful. As the QPY is
185
+ backwards compatible but not forwards compatible you need to ensure a given
186
+ QPY format version was released in the release you're calling :func:`.load`
187
+ with. The following table lists the QPY versions that were supported in every
188
+ Qiskit (and qiskit-terra prior to Qiskit 1.0.0) release going back to the introduction
189
+ of QPY in qiskit-terra 0.18.0.
190
+
191
+ .. list-table:: QPY Format Version History
192
+ :header-rows: 1
193
+
194
+ * - Qiskit (qiskit-terra for < 1.0.0) version
195
+ - :func:`.dump` format(s) output versions
196
+ - :func:`.load` maximum supported version (older format versions can always be read)
197
+ * - 2.0.0
198
+ - 13, 14
199
+ - 14
200
+ * - 1.4.2
201
+ - 10, 11, 12, 13
202
+ - 13
203
+ * - 1.4.1
204
+ - 10, 11, 12, 13
205
+ - 13
206
+ * - 1.4.0
207
+ - 10, 11, 12, 13
208
+ - 13
209
+ * - 1.3.3
210
+ - 10, 11, 12, 13
211
+ - 13
212
+ * - 1.3.2
213
+ - 10, 11, 12, 13
214
+ - 13
215
+ * - 1.3.1
216
+ - 10, 11, 12, 13
217
+ - 13
218
+ * - 1.3.0
219
+ - 10, 11, 12, 13
220
+ - 13
221
+ * - 1.2.4
222
+ - 10, 11, 12
223
+ - 12
224
+ * - 1.2.3 (yanked)
225
+ - 10, 11, 12
226
+ - 12
227
+ * - 1.2.2
228
+ - 10, 11, 12
229
+ - 12
230
+ * - 1.2.1
231
+ - 10, 11, 12
232
+ - 12
233
+ * - 1.2.0
234
+ - 10, 11, 12
235
+ - 12
236
+ * - 1.1.0
237
+ - 10, 11, 12
238
+ - 12
239
+ * - 1.0.2
240
+ - 10, 11
241
+ - 11
242
+ * - 1.0.1
243
+ - 10, 11
244
+ - 11
245
+ * - 1.0.0
246
+ - 10, 11
247
+ - 11
248
+ * - 0.46.1
249
+ - 10
250
+ - 10
251
+ * - 0.45.3
252
+ - 10
253
+ - 10
254
+ * - 0.45.2
255
+ - 10
256
+ - 10
257
+ * - 0.45.1
258
+ - 10
259
+ - 10
260
+ * - 0.45.0
261
+ - 10
262
+ - 10
263
+ * - 0.25.3
264
+ - 9
265
+ - 9
266
+ * - 0.25.2
267
+ - 9
268
+ - 9
269
+ * - 0.25.1
270
+ - 9
271
+ - 9
272
+ * - 0.24.2
273
+ - 8
274
+ - 8
275
+ * - 0.24.1
276
+ - 7
277
+ - 7
278
+ * - 0.24.0
279
+ - 7
280
+ - 7
281
+ * - 0.23.3
282
+ - 6
283
+ - 6
284
+ * - 0.23.2
285
+ - 6
286
+ - 6
287
+ * - 0.23.1
288
+ - 6
289
+ - 6
290
+ * - 0.23.0
291
+ - 6
292
+ - 6
293
+ * - 0.22.4
294
+ - 5
295
+ - 5
296
+ * - 0.22.3
297
+ - 5
298
+ - 5
299
+ * - 0.22.2
300
+ - 5
301
+ - 5
302
+ * - 0.22.1
303
+ - 5
304
+ - 5
305
+ * - 0.22.0
306
+ - 5
307
+ - 5
308
+ * - 0.21.2
309
+ - 5
310
+ - 5
311
+ * - 0.21.1
312
+ - 5
313
+ - 5
314
+ * - 0.21.0
315
+ - 5
316
+ - 5
317
+ * - 0.20.2
318
+ - 4
319
+ - 4
320
+ * - 0.20.1
321
+ - 4
322
+ - 4
323
+ * - 0.20.0
324
+ - 4
325
+ - 4
326
+ * - 0.19.2
327
+ - 4
328
+ - 4
329
+ * - 0.19.1
330
+ - 3
331
+ - 3
332
+ * - 0.19.0
333
+ - 2
334
+ - 2
335
+ * - 0.18.3
336
+ - 1
337
+ - 1
338
+ * - 0.18.2
339
+ - 1
340
+ - 1
341
+ * - 0.18.1
342
+ - 1
343
+ - 1
344
+ * - 0.18.0
345
+ - 1
346
+ - 1
347
+
348
+ .. _qpy_format:
349
+
350
+ QPY Format
351
+ ==========
352
+
353
+ The QPY serialization format is a portable cross-platform binary
354
+ serialization format for :class:`~qiskit.circuit.QuantumCircuit` objects in Qiskit. The basic
355
+ file format is as follows:
356
+
357
+ A QPY file (or memory object) always starts with the following 6
358
+ byte UTF8 string: ``QISKIT`` which is immediately followed by the overall
359
+ file header. The contents of the file header as defined as a C struct are:
360
+
361
+ .. code-block:: c
362
+
363
+ struct {
364
+ uint8_t qpy_version;
365
+ uint8_t qiskit_major_version;
366
+ uint8_t qiskit_minor_version;
367
+ uint8_t qiskit_patch_version;
368
+ uint64_t num_circuits;
369
+ }
370
+
371
+
372
+ From V10 on, a new field is added to the file header struct to represent the
373
+ encoding scheme used for symbolic expressions:
374
+
375
+ .. code-block:: c
376
+
377
+ struct {
378
+ uint8_t qpy_version;
379
+ uint8_t qiskit_major_version;
380
+ uint8_t qiskit_minor_version;
381
+ uint8_t qiskit_patch_version;
382
+ uint64_t num_circuits;
383
+ char symbolic_encoding;
384
+ }
385
+
386
+ All values use network byte order [#f1]_ (big endian) for cross platform
387
+ compatibility.
388
+
389
+ The file header is immediately followed by the circuit payloads.
390
+ Each individual circuit is composed of the following parts:
391
+
392
+ ``HEADER | METADATA | REGISTERS | STANDALONE_VARS | CUSTOM_DEFINITIONS | INSTRUCTIONS``
393
+
394
+ The ``STANDALONE_VARS`` are new in QPY version 12; before that, there was no data between
395
+ ``REGISTERS`` and ``CUSTOM_DEFINITIONS``.
396
+
397
+ There is a circuit payload for each circuit (where the total number is dictated
398
+ by ``num_circuits`` in the file header). There is no padding between the
399
+ circuits in the data.
400
+
401
+ .. _qpy_version_14:
402
+
403
+ Version 14
404
+ ----------
405
+
406
+ Version 14 adds a new core DURATION type, support for additional :class:`~.types.Type`
407
+ classes :class:`~.types.Float` and :class:`~.types.Duration`, and a new expression
408
+ node type :class:`~.expr.Stretch`.
409
+
410
+ DURATION
411
+ ~~~~~~~~
412
+
413
+ A :class:`~.circuit.Duration` is encoded by a single-byte ASCII ``char`` that encodes the kind of
414
+ type, followed by a payload that varies depending on the type. The defined codes are:
415
+
416
+ ============================== ========= =========================================================
417
+ Qiskit class Type code Payload
418
+ ============================== ========= =========================================================
419
+ :class:`~.circuit.Duration.dt` ``t`` One ``unsigned long long value``.
420
+
421
+ :class:`~.circuit.Duration.ns` ``n`` One ``double value``.
422
+
423
+ :class:`~.circuit.Duration.us` ``u`` One ``double value``.
424
+
425
+ :class:`~.circuit.Duration.ms` ``m`` One ``double value``.
426
+
427
+ :class:`~.circuit.Duration.s` ``s`` One ``double value``.
428
+
429
+ ============================== ========= =========================================================
430
+
431
+ Changes to EXPR_VAR_DECLARATION
432
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
433
+
434
+ The ``EXPR_VAR_DECLARATION`` type is now used to represent both :class:`~.expr.Var` standalone
435
+ variables and :class:`~.expr.Stretch` identifiers. To support this change, the usage type code has
436
+ two new possible entries, in addition to the existing ones:
437
+
438
+ ========= =========================================================================================
439
+ Type code Meaning
440
+ ========= =========================================================================================
441
+ ``A`` A ``capture`` stretch to the circuit.
442
+
443
+ ``O`` A locally declared stretch to the circuit.
444
+
445
+ ========= =========================================================================================
446
+
447
+ Changes to EXPRESSION
448
+ ---------------------
449
+
450
+ The EXPRESSION type code has a new possible entry, ``s``, corresponding to :class:`.expr.Stretch`
451
+ nodes.
452
+
453
+ ======================= ========= ====================================================== ========
454
+ Qiskit class Type code Payload Children
455
+ ======================= ========= ====================================================== ========
456
+ :class:`~.expr.Stretch` ``s`` One ``unsigned short var_index`` 0
457
+ ======================= ========= ====================================================== ========
458
+
459
+ Changes to EXPR_TYPE
460
+ ~~~~~~~~~~~~~~~~~~~~
461
+
462
+ The following table shows the new type classes added in the version:
463
+
464
+ ========================= ========= ==============================================================
465
+ Qiskit class Type code Payload
466
+ ========================= ========= ==============================================================
467
+ :class:`~.types.Float` ``f`` None.
468
+
469
+ :class:`~.types.Duration` ``d`` None.
470
+
471
+ ========================= ========= ==============================================================
472
+
473
+ Changes to EXPR_VALUE
474
+ ~~~~~~~~~~~~~~~~~~~~~
475
+
476
+ The classical expression's type system now supports new encoding types for value literals, in
477
+ addition to the existing encodings for int and bool. The new value type encodings are below:
478
+
479
+ =========================== ========= ============================================================
480
+ Python type Type code Payload
481
+ =========================== ========= ============================================================
482
+ ``float`` ``f`` One ``double value``.
483
+
484
+ :class:`~.circuit.Duration` ``t`` One ``DURATION``.
485
+
486
+ =========================== ========= ============================================================
487
+
488
+ .. _qpy_version_13:
489
+
490
+ Version 13
491
+ ----------
492
+
493
+ Version 13 added a native Qiskit serialization representation for :class:`.ParameterExpression`.
494
+ Previous QPY versions relied on either ``sympy`` or ``symengine`` to serialize the underlying symbolic
495
+ expression. Starting in Version 13, QPY now represents the sequence of API calls used to create the
496
+ :class:`.ParameterExpression`.
497
+
498
+ The main change in the serialization format is in the :ref:`qpy_param_expr_v3` payload. The
499
+ ``expr_size`` bytes following the head now contain an array of ``PARAM_EXPR_ELEM_V13`` structs. The
500
+ intent is for this array to be read one struct at a time, where each struct describes one of the
501
+ calls to make to reconstruct the :class:`.ParameterExpression`.
502
+
503
+ PARAM_EXPR_ELEM_V13
504
+ ~~~~~~~~~~~~~~~~~~~
505
+
506
+ The struct format is defined as:
507
+
508
+ .. code-block:: c
509
+
510
+ struct {
511
+ unsigned char op_code;
512
+ char lhs_type;
513
+ char lhs[16];
514
+ char rhs_type;
515
+ char rhs[16];
516
+ } PARAM_EXPR_ELEM_V13;
517
+
518
+ The ``op_code`` field is used to define the operation added to the :class:`.ParameterExpression`.
519
+ The value can be:
520
+
521
+ .. list-table:: PARAM_EXPR_ELEM_V13 op code values
522
+ :header-rows: 1
523
+
524
+ * - ``op_code``
525
+ - :class:`.ParameterExpression` method
526
+ * - 0
527
+ - :meth:`~.ParameterExpression.__add__`
528
+ * - 1
529
+ - :meth:`~.ParameterExpression.__sub__`
530
+ * - 2
531
+ - :meth:`~.ParameterExpression.__mul__`
532
+ * - 3
533
+ - :meth:`~.ParameterExpression.__truediv__`
534
+ * - 4
535
+ - :meth:`~.ParameterExpression.__pow__`
536
+ * - 5
537
+ - :meth:`~.ParameterExpression.sin`
538
+ * - 6
539
+ - :meth:`~.ParameterExpression.cos`
540
+ * - 7
541
+ - :meth:`~.ParameterExpression.tan`
542
+ * - 8
543
+ - :meth:`~.ParameterExpression.arcsin`
544
+ * - 9
545
+ - :meth:`~.ParameterExpression.arccos`
546
+ * - 10
547
+ - :meth:`~.ParameterExpression.exp`
548
+ * - 11
549
+ - :meth:`~.ParameterExpression.log`
550
+ * - 12
551
+ - :meth:`~.ParameterExpression.sign`
552
+ * - 13
553
+ - :meth:`~.ParameterExpression.gradient`
554
+ * - 14
555
+ - :meth:`~.ParameterExpression.conjugate`
556
+ * - 15
557
+ - :meth:`~.ParameterExpression.subs`
558
+ * - 16
559
+ - :meth:`~.ParameterExpression.abs`
560
+ * - 17
561
+ - :meth:`~.ParameterExpression.arctan`
562
+ * - 255
563
+ - NULL
564
+
565
+ The ``NULL`` value of 255 is only used to fill the op code field for
566
+ entries that are not actual operations but indicate recursive definitions.
567
+ Then the ``lhs_type`` and ``rhs_type`` fields are used to describe
568
+ the operand types and can be one of the following UTF-8 encoded
569
+ characters:
570
+
571
+ .. list-table:: PARAM_EXPR_ELEM_V13 operand type values
572
+ :header-rows: 1
573
+
574
+ * - Value
575
+ - Type
576
+ * - ``n``
577
+ - ``None``
578
+ * - ``p``
579
+ - :class:`.Parameter`
580
+ * - ``f``
581
+ - ``float``
582
+ * - ``c``
583
+ - ``complex``
584
+ * - ``i``
585
+ - ``int``
586
+ * - ``s``
587
+ - Recursive :class:`.ParameterExpression` definition start
588
+ * - ``e``
589
+ - Recursive :class:`.ParameterExpression` definition stop
590
+ * - ``u``
591
+ - substitution
592
+
593
+ If the type value is ``f``, ``c``, or ``i``, the corresponding ``lhs`` or ``rhs``
594
+ field widths are 128 bits each. In the case of floats, the literal value is encoded as a double
595
+ with 0 padding, while complex numbers are encoded as real part followed by imaginary part,
596
+ taking up 64 bits each. For ``i``, the value is encoded as a 64 bit signed integer with 0 padding
597
+ for the full 128 bit width. ``n`` is used to represent a ``None`` and typically isn't directly used
598
+ as it indicates an argument that's not used. For ``p`` the data is the UUID for the
599
+ :class:`.Parameter` which can be looked up in the symbol map described in the
600
+ ``map_elements`` outer :ref:`qpy_param_expr_v3` payload. If the type value is
601
+ ``s`` this marks the start of a a new recursive section for a nested
602
+ :class:`.ParameterExpression`. For example, in the following snippet there is an inner ``expr``
603
+ contained in ``final_expr``, constituting a nested expression::
604
+
605
+ from qiskit.circuit import Parameter
606
+
607
+ x = Parameter("x")
608
+ y = Parameter("y")
609
+ z = Parameter("z")
610
+
611
+ expr = (x + y) / 2
612
+ final_expr = z**2 + expr
613
+
614
+ When ``s`` is encountered, this indicates that until an ``e` struct is reached, the next structs
615
+ are used for a recursive definition. For both
616
+ ``s`` and ``e`` types, the data values are not used, and always set to 0. The type value
617
+ of ``u`` is used to represent a substitution call. This is only used for ``lhs_type``
618
+ and is always paired with an ``rhs_type`` of ``n``. The data value is the size in bytes of
619
+ a :ref:`qpy_mapping` encoded mapping of :class:`.Parameter` names to their value for the
620
+ :meth:`~.ParameterExpression.subs` call. The mapping data is immediately following the
621
+ struct, and the next struct starts immediately after the mapping data.
622
+
623
+ .. _qpy_version_12:
624
+
625
+ Version 12
626
+ ----------
627
+
628
+ Version 12 adds support for:
629
+
630
+ * circuits containing memory-owning :class:`.expr.Var` variables.
631
+
632
+ Changes to HEADER
633
+ ~~~~~~~~~~~~~~~~~
634
+
635
+ The HEADER struct for an individual circuit has added three ``uint32_t`` counts of the input,
636
+ captured and locally declared variables in the circuit. The new form looks like:
637
+
638
+ .. code-block:: c
639
+
640
+ struct {
641
+ uint16_t name_size;
642
+ char global_phase_type;
643
+ uint16_t global_phase_size;
644
+ uint32_t num_qubits;
645
+ uint32_t num_clbits;
646
+ uint64_t metadata_size;
647
+ uint32_t num_registers;
648
+ uint64_t num_instructions;
649
+ uint32_t num_vars;
650
+ } HEADER_V12;
651
+
652
+ The ``HEADER_V12`` struct is followed immediately by the same name, global-phase, metadata
653
+ and register information as the V2 version of the header. Immediately following the registers is
654
+ ``num_vars`` instances of ``EXPR_VAR_STANDALONE`` that define the variables in this circuit. After
655
+ that, the data continues with custom definitions and instructions as in prior versions of QPY.
656
+
657
+
658
+ EXPR_VAR_DECLARATION
659
+ ~~~~~~~~~~~~~~~~~~~~
660
+
661
+ An ``EXPR_VAR_DECLARATION`` defines an :class:`.expr.Var` instance that is standalone; that is, it
662
+ represents a self-owned memory location rather than wrapping a :class:`.Clbit` or
663
+ :class:`.ClassicalRegister`. The payload is a C struct:
664
+
665
+ .. code-block:: c
666
+
667
+ struct {
668
+ char uuid_bytes[16];
669
+ char usage;
670
+ uint16_t name_size;
671
+ }
672
+
673
+ which is immediately followed by an ``EXPR_TYPE`` payload and then ``name_size`` bytes of UTF-8
674
+ encoding string data containing the name of the variable.
675
+
676
+ The ``char`` usage type code takes the following values:
677
+
678
+ ========= =========================================================================================
679
+ Type code Meaning
680
+ ========= =========================================================================================
681
+ ``I`` An ``input`` variable to the circuit.
682
+
683
+ ``C`` A ``capture`` variable to the circuit.
684
+
685
+ ``L`` A locally declared variable to the circuit.
686
+ ========= =========================================================================================
687
+
688
+
689
+ Changes to EXPR_VAR
690
+ ~~~~~~~~~~~~~~~~~~~
691
+
692
+ The ``EXPR_VAR`` variable has gained a new type code and payload, in addition to the pre-existing ones:
693
+
694
+ =========================== ========= ============================================================
695
+ Python class Type code Payload
696
+ =========================== ========= ============================================================
697
+ :class:`.UUID` ``U`` One ``uint32_t`` index of the variable into the series of
698
+ ``EXPR_VAR_STANDALONE`` variables that were written
699
+ immediately after the circuit header.
700
+ =========================== ========= ============================================================
701
+
702
+ Notably, this new type-code indexes into pre-defined variables from the circuit header, rather than
703
+ redefining the variable again in each location it is used.
704
+
705
+
706
+ Changes to EXPRESSION
707
+ ---------------------
708
+
709
+ The EXPRESSION type code has a new possible entry, ``i``, corresponding to :class:`.expr.Index`
710
+ nodes.
711
+
712
+ ====================== ========= ======================================================= ========
713
+ Qiskit class Type code Payload Children
714
+ ====================== ========= ======================================================= ========
715
+ :class:`~.expr.Index` ``i`` No additional payload. The children are the target 2
716
+ and the index, in that order.
717
+ ====================== ========= ======================================================= ========
718
+
719
+
720
+ .. _qpy_version_11:
721
+
722
+ Version 11
723
+ ----------
724
+
725
+ Version 11 is identical to Version 10 except for the following.
726
+ First, the names in the CUSTOM_INSTRUCTION blocks
727
+ have a suffix of the form ``"_{uuid_hex}"`` where ``uuid_hex`` is a uuid
728
+ hexadecimal string such as returned by :attr:`.UUID.hex`. For example:
729
+ ``"b3ecab5b4d6a4eb6bc2b2dbf18d83e1e"``.
730
+ Second, it adds support for :class:`.AnnotatedOperation`
731
+ objects. The base operation of an annotated operation is stored using the INSTRUCTION block,
732
+ and an additional ``type`` value ``'a'``is added to indicate that the custom instruction is an
733
+ annotated operation. The list of modifiers are stored as instruction parameters using INSTRUCTION_PARAM,
734
+ with an additional value ``'m'`` is added to indicate that the parameter is of type
735
+ :class:`~qiskit.circuit.annotated_operation.Modifier`. Each modifier is stored using the
736
+ MODIFIER struct.
737
+
738
+ .. _modifier_qpy:
739
+
740
+ MODIFIER
741
+ ~~~~~~~~
742
+
743
+ This represents :class:`~qiskit.circuit.annotated_operation.Modifier`
744
+
745
+ .. code-block:: c
746
+
747
+ struct {
748
+ char type;
749
+ uint32_t num_ctrl_qubits;
750
+ uint32_t ctrl_state;
751
+ double power;
752
+ }
753
+
754
+ This is sufficient to store different types of modifiers required for serializing objects
755
+ of type :class:`.AnnotatedOperation`.
756
+ The field ``type`` is either ``'i'``, ``'c'`` or ``'p'``, representing whether the modifier
757
+ is respectively an inverse modifier, a control modifier or a power modifier. In the second
758
+ case, the fields ``num_ctrl_qubits`` and ``ctrl_state`` specify the control logic of the base
759
+ operation, and in the third case the field ``power`` represents the power of the base operation.
760
+
761
+ .. _qpy_version_10:
762
+
763
+ Version 10
764
+ ----------
765
+
766
+ Version 10 adds support for:
767
+
768
+ * symengine-native serialization for objects of type :class:`~.ParameterExpression` as well as
769
+ symbolic expressions in Pulse schedule blocks.
770
+ * new fields in the :class:`~.TranspileLayout` class added in the Qiskit 0.45.0 release.
771
+
772
+ The symbolic_encoding field is added to the file header, and a new encoding type char
773
+ is introduced, mapped to each symbolic library as follows: ``p`` refers to sympy
774
+ encoding and ``e`` refers to symengine encoding.
775
+
776
+ Changes to FILE_HEADER
777
+ ~~~~~~~~~~~~~~~~~~~~~~
778
+
779
+ The contents of FILE_HEADER after V10 are defined as a C struct as:
780
+
781
+ .. code-block:: c
782
+
783
+ struct {
784
+ uint8_t qpy_version;
785
+ uint8_t qiskit_major_version;
786
+ uint8_t qiskit_minor_version;
787
+ uint8_t qiskit_patch_version;
788
+ uint64_t num_circuits;
789
+ char symbolic_encoding;
790
+ } FILE_HEADER_V10;
791
+
792
+ Changes to LAYOUT
793
+ ~~~~~~~~~~~~~~~~~
794
+
795
+ The ``LAYOUT`` struct is updated to have an additional ``input_qubit_count`` field.
796
+ With version 10 the ``LAYOUT`` struct is now:
797
+
798
+ .. code-block:: c
799
+
800
+ struct {
801
+ char exists;
802
+ int32_t initial_layout_size;
803
+ int32_t input_mapping_size;
804
+ int32_t final_layout_size;
805
+ uint32_t extra_registers;
806
+ int32_t input_qubit_count;
807
+ }
808
+
809
+ The rest of the layout data after the ``LAYOUT`` struct is represented as in previous versions. If
810
+ ``input qubit_count`` is < 0 that indicates that both ``_input_qubit_count``
811
+ and ``_output_qubit_list`` in the :class:`~.TranspileLayout` object are ``None``.
812
+
813
+ .. _qpy_version_9:
814
+
815
+ Version 9
816
+ ---------
817
+
818
+ Version 9 adds support for classical :class:`~.expr.Expr` nodes and their associated
819
+ :class:`~.types.Type`\\ s.
820
+
821
+
822
+ EXPRESSION
823
+ ~~~~~~~~~~
824
+
825
+ An :class:`~.expr.Expr` node is represented by a stream of variable-width data. A node itself is
826
+ represented by (in order in the byte stream):
827
+
828
+ #. a one-byte type code discriminator;
829
+ #. an EXPR_TYPE object;
830
+ #. a type-code-specific additional payload;
831
+ #. a type-code-specific number of child EXPRESSION payloads (the number of these is implied by the
832
+ type code and not explicitly stored).
833
+
834
+ Each of these are described in the following table:
835
+
836
+ ====================== ========= ======================================================= ========
837
+ Qiskit class Type code Payload Children
838
+ ====================== ========= ======================================================= ========
839
+ :class:`~.expr.Var` ``x`` One ``EXPR_VAR``. 0
840
+
841
+ :class:`~.expr.Value` ``v`` One ``EXPR_VALUE``. 0
842
+
843
+ :class:`~.expr.Cast` ``c`` One ``_Bool`` that corresponds to the value of 1
844
+ ``implicit``.
845
+
846
+ :class:`~.expr.Unary` ``u`` One ``uint8_t`` with the same numeric value as the 1
847
+ :class:`.Unary.Op`.
848
+
849
+ :class:`~.expr.Binary` ``b`` One ``uint8_t`` with the same numeric value as the 2
850
+ :class:`.Binary.Op`.
851
+ ====================== ========= ======================================================= ========
852
+
853
+
854
+ EXPR_TYPE
855
+ ~~~~~~~~~
856
+
857
+ A :class:`~.types.Type` is encoded by a single-byte ASCII ``char`` that encodes the kind of type,
858
+ followed by a payload that varies depending on the type. The defined codes are:
859
+
860
+ ====================== ========= =================================================================
861
+ Qiskit class Type code Payload
862
+ ====================== ========= =================================================================
863
+ :class:`~.types.Bool` ``b`` None.
864
+
865
+ :class:`~.types.Uint` ``u`` One ``uint32_t width``.
866
+ ====================== ========= =================================================================
867
+
868
+
869
+ EXPR_VAR
870
+ ~~~~~~~~
871
+
872
+ This represents a runtime variable of a :class:`~.expr.Var` node. These are a type code, followed
873
+ by a type-code-specific payload:
874
+
875
+ =========================== ========= ============================================================
876
+ Python class Type code Payload
877
+ =========================== ========= ============================================================
878
+ :class:`.Clbit` ``C`` One ``uint32_t index`` that is the index of the
879
+ :class:`.Clbit` in the containing circuit.
880
+
881
+ :class:`.ClassicalRegister` ``R`` One ``uint16_t reg_name_size``, followed by that many bytes
882
+ of UTF-8 string data of the register name.
883
+ =========================== ========= ============================================================
884
+
885
+
886
+ EXPR_VALUE
887
+ ~~~~~~~~~~
888
+
889
+ This represents a literal object in the classical type system, such as an integer. Currently there
890
+ are very few such literals. These are encoded as a type code, followed by a type-code-specific
891
+ payload.
892
+
893
+ =========== ========= ============================================================================
894
+ Python type Type code Payload
895
+ =========== ========= ============================================================================
896
+ ``bool`` ``b`` One ``_Bool value``.
897
+
898
+ ``int`` ``i`` One ``uint8_t num_bytes``, followed by the integer encoded into that many
899
+ many bytes (network order) in a two's complement representation.
900
+ =========== ========= ============================================================================
901
+
902
+
903
+
904
+ Changes to INSTRUCTION
905
+ ~~~~~~~~~~~~~~~~~~~~~~
906
+
907
+ To support the use of :class:`~.expr.Expr` nodes in the fields :attr:`.IfElseOp.condition`,
908
+ :attr:`.WhileLoopOp.condition` and :attr:`.SwitchCaseOp.target`, the INSTRUCTION struct is changed
909
+ in an ABI compatible-manner to :ref:`its previous definition <qpy_instruction_v5>`. The new struct
910
+ is the C struct:
911
+
912
+ .. code-block:: c
913
+
914
+ struct {
915
+ uint16_t name_size;
916
+ uint16_t label_size;
917
+ uint16_t num_parameters;
918
+ uint32_t num_qargs;
919
+ uint32_t num_cargs;
920
+ uint8_t conditional_key;
921
+ uint16_t conditional_reg_name_size;
922
+ int64_t conditional_value;
923
+ uint32_t num_ctrl_qubits;
924
+ uint32_t ctrl_state;
925
+ }
926
+
927
+ where the only change is that a ``uint8_t conditional_key`` entry has replaced ``_Bool
928
+ has_conditional``. This new ``conditional_key`` takes the following numeric values, with these
929
+ effects:
930
+
931
+ ===== =============================================================================================
932
+ Value Effects
933
+ ===== =============================================================================================
934
+ 0 The instruction has its ``.condition`` field set to ``None``. The
935
+ ``conditional_reg_name_size`` and ``conditional_value`` fields should be ignored.
936
+
937
+ 1 The instruction has its ``.condition`` field set to a 2-tuple of either a :class:`.Clbit`
938
+ or a :class:`.ClassicalRegister`, and a integer of value ``conditional_value``. The
939
+ INSTRUCTION payload, including its trailing data is parsed exactly as it would be in QPY
940
+ versions less than 8.
941
+
942
+ 2 The instruction has its ``.condition`` field set to a :class:`~.expr.Expr` node. The
943
+ ``conditional_reg_name_size`` and ``conditional_value`` fields should be ignored. The data
944
+ following the struct is followed (as in QPY versions less than 8) by ``name_size`` bytes of
945
+ UTF-8 string data for the class name and ``label_size`` bytes of UTF-8 string data for the
946
+ label (if any). Then, there is one INSTRUCTION_PARAM, which will contain an EXPRESSION. After
947
+ that, parsing continues with the INSTRUCTION_ARG structs, as in previous versions of QPY.
948
+ ===== =============================================================================================
949
+
950
+
951
+ Changes to INSTRUCTION_PARAM
952
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
953
+
954
+ A new type code ``x`` is added that defines an EXPRESSION parameter.
955
+
956
+
957
+ .. _qpy_version_8:
958
+
959
+ Version 8
960
+ ---------
961
+
962
+ Version 8 adds support for handling a :class:`~.TranspileLayout` stored in the
963
+ :attr:`.QuantumCircuit.layout` attribute. In version 8 immediately following the
964
+ calibrations block at the end of the circuit payload there is now the
965
+ ``LAYOUT`` struct. This struct outlines the size of the three attributes of a
966
+ :class:`~.TranspileLayout` class.
967
+
968
+ LAYOUT
969
+ ~~~~~~
970
+
971
+ .. code-block:: c
972
+
973
+ struct {
974
+ char exists;
975
+ int32_t initial_layout_size;
976
+ int32_t input_mapping_size;
977
+ int32_t final_layout_size;
978
+ uint32_t extra_registers;
979
+ }
980
+
981
+ If any of the signed values are ``-1`` this indicates the corresponding
982
+ attribute is ``None``.
983
+
984
+ Immediately following the ``LAYOUT`` struct there is a :ref:`qpy_registers` struct
985
+ for ``extra_registers`` (specifically the format introduced in :ref:`qpy_version_4`)
986
+ standalone register definitions that aren't present in the circuit. Then there
987
+ are ``initial_layout_size`` ``INITIAL_LAYOUT_BIT`` structs to define the
988
+ :attr:`.TranspileLayout.initial_layout` attribute.
989
+
990
+ INITIAL_LAYOUT_BIT
991
+ ~~~~~~~~~~~~~~~~~~
992
+
993
+ .. code-block:: c
994
+
995
+ struct {
996
+ int32_t index;
997
+ int32_t register_size;
998
+ }
999
+
1000
+ Where a value of ``-1`` indicates ``None`` (as in no register is associated
1001
+ with the bit). Following each ``INITIAL_LAYOUT_BIT`` struct is ``register_size``
1002
+ bytes for a ``utf8`` encoded string for the register name.
1003
+
1004
+ Following the initial layout there is ``input_mapping_size`` array of
1005
+ ``uint32_t`` integers representing the positions of the physical bit from the
1006
+ initial layout. This enables constructing a list of virtual bits where the
1007
+ array index is its input mapping position.
1008
+
1009
+ Finally, there is an array of ``final_layout_size`` ``uint32_t`` integers. Each
1010
+ element is an index in the circuit's ``qubits`` attribute which enables building
1011
+ a mapping from qubit starting position to the output position at the end of the
1012
+ circuit.
1013
+
1014
+ .. _qpy_version_7:
1015
+
1016
+ Version 7
1017
+ ---------
1018
+
1019
+ Version 7 adds support for :class:`.~Reference` instruction and serialization of
1020
+ a ``ScheduleBlock`` program while keeping its reference to subroutines::
1021
+
1022
+ from qiskit import pulse
1023
+ from qiskit import qpy
1024
+
1025
+ with pulse.build() as schedule:
1026
+ pulse.reference("cr45p", "q0", "q1")
1027
+ pulse.reference("x", "q0")
1028
+ pulse.reference("cr45p", "q0", "q1")
1029
+
1030
+ with open('template_ecr.qpy', 'wb') as fd:
1031
+ qpy.dump(schedule, fd)
1032
+
1033
+ The conventional :ref:`qpy_schedule_block` data model is preserved, but in
1034
+ version 7 it is immediately followed by an extra :ref:`qpy_mapping` utf8 bytes block
1035
+ representing the data of the referenced subroutines.
1036
+
1037
+ New type key character is added to the :ref:`qpy_schedule_instructions` group
1038
+ for the :class:`.~Reference` instruction.
1039
+
1040
+ - ``y``: :class:`~qiskit.pulse.instructions.Reference` instruction
1041
+
1042
+ New type key character is added to the :ref:`qpy_schedule_operands` group
1043
+ for the operands of :class:`.~Reference` instruction,
1044
+ which is a tuple of strings, e.g. ("cr45p", "q0", "q1").
1045
+
1046
+ - ``o``: string (operand string)
1047
+
1048
+ Note that this is the same encoding with the built-in Python string, however,
1049
+ the standard value encoding in QPY uses ``s`` type character for string data,
1050
+ which conflicts with the :class:`~qiskit.pulse.library.SymbolicPulse` in the scope of
1051
+ pulse instruction operands. A special type character ``o`` is reserved for
1052
+ the string data that appears in the pulse instruction operands.
1053
+
1054
+ In addition, version 7 adds two new type keys to the INSTRUCTION_PARM struct. ``"d"`` is followed
1055
+ by no data and represents the literal value :data:`.CASE_DEFAULT` for switch-statement support.
1056
+ ``"R"`` represents a :class:`.ClassicalRegister` or :class:`.Clbit`, and is followed by the same
1057
+ format as the description of register or classical bit as used in the first element of :ref:`the
1058
+ condition of an INSTRUCTION field <qpy_instructions>`.
1059
+
1060
+ .. _qpy_version_6:
1061
+
1062
+ Version 6
1063
+ ---------
1064
+
1065
+ Version 6 adds support for :class:`.~ScalableSymbolicPulse`. These objects are saved and read
1066
+ like `SymbolicPulse` objects, and the class name is added to the data to correctly handle
1067
+ the class selection.
1068
+
1069
+ `SymbolicPulse` block now starts with SYMBOLIC_PULSE_V2 header:
1070
+
1071
+ .. code-block:: c
1072
+
1073
+ struct {
1074
+ uint16_t class_name_size;
1075
+ uint16_t type_size;
1076
+ uint16_t envelope_size;
1077
+ uint16_t constraints_size;
1078
+ uint16_t valid_amp_conditions_size;
1079
+ _bool amp_limited;
1080
+ }
1081
+
1082
+ The only change compared to :ref:`qpy_version_5` is the addition of `class_name_size`. The header
1083
+ is then immediately followed by ``class_name_size`` utf8 bytes with the name of the class. Currently,
1084
+ either `SymbolicPulse` or `ScalableSymbolicPulse` are supported. The rest of the data is then
1085
+ identical to :ref:`qpy_version_5`.
1086
+
1087
+ .. _qpy_version_5:
1088
+
1089
+ Version 5
1090
+ ---------
1091
+
1092
+ Version 5 changes from :ref:`qpy_version_4` by adding support for ``ScheduleBlock``
1093
+ and changing two payloads the INSTRUCTION metadata payload and the CUSTOM_INSTRUCTION block.
1094
+ These now have new fields to better account for :class:`~.ControlledGate` objects in a circuit.
1095
+ In addition, new payload MAP_ITEM is defined to implement the :ref:`qpy_mapping` block.
1096
+
1097
+ With the support of ``ScheduleBlock``, now :class:`~.QuantumCircuit` can be
1098
+ serialized together with :attr:`~.QuantumCircuit.calibrations`, or
1099
+ `Pulse Gates <https://quantum.cloud.ibm.com/docs/guides/pulse>`_.
1100
+ In QPY version 5 and above, :ref:`qpy_circuit_calibrations` payload is
1101
+ packed after the :ref:`qpy_instructions` block.
1102
+
1103
+ In QPY version 5 and above,
1104
+
1105
+ .. code-block:: c
1106
+
1107
+ struct {
1108
+ char type;
1109
+ }
1110
+
1111
+ immediately follows the file header block to represent the program type stored in the file.
1112
+
1113
+ - When ``type==c``, :class:`~.QuantumCircuit` payload follows
1114
+ - When ``type==s``, ``ScheduleBlock`` payload follows
1115
+
1116
+ .. note::
1117
+
1118
+ Different programs cannot be packed together in the same file.
1119
+ You must create different files for different program types.
1120
+ Multiple objects with the same type can be saved in a single file.
1121
+
1122
+ .. _qpy_schedule_block:
1123
+
1124
+ SCHEDULE_BLOCK
1125
+ ~~~~~~~~~~~~~~
1126
+
1127
+ ``ScheduleBlock`` is first supported in QPY Version 5. This allows
1128
+ users to save pulse programs in the QPY binary format as follows:
1129
+
1130
+ .. code-block:: python
1131
+
1132
+ from qiskit import pulse, qpy
1133
+
1134
+ with pulse.build() as schedule:
1135
+ pulse.play(pulse.Gaussian(160, 0.1, 40), pulse.DriveChannel(0))
1136
+
1137
+ with open('schedule.qpy', 'wb') as fd:
1138
+ qpy.dump(schedule, fd)
1139
+
1140
+ with open('schedule.qpy', 'rb') as fd:
1141
+ new_schedule = qpy.load(fd)[0]
1142
+
1143
+ Note that circuit and schedule block are serialized and deserialized through
1144
+ the same QPY interface. Input data type is implicitly analyzed and
1145
+ no extra option is required to save the schedule block.
1146
+
1147
+ .. _qpy_schedule_block_header:
1148
+
1149
+ SCHEDULE_BLOCK_HEADER
1150
+ ~~~~~~~~~~~~~~~~~~~~~
1151
+
1152
+ ``ScheduleBlock`` block starts with the following header:
1153
+
1154
+ .. code-block:: c
1155
+
1156
+ struct {
1157
+ uint16_t name_size;
1158
+ uint64_t metadata_size;
1159
+ uint16_t num_element;
1160
+ }
1161
+
1162
+ which is immediately followed by ``name_size`` utf8 bytes of schedule name and
1163
+ ``metadata_size`` utf8 bytes of the JSON serialized metadata dictionary
1164
+ attached to the schedule.
1165
+
1166
+ .. _qpy_schedule_alignments:
1167
+
1168
+ SCHEDULE_BLOCK_ALIGNMENTS
1169
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
1170
+
1171
+ Then, alignment context of the schedule block starts with ``char``
1172
+ representing the supported context type followed by the :ref:`qpy_sequence` block representing
1173
+ the parameters associated with the alignment context :attr:`AlignmentKind._context_params`.
1174
+ The context type char is mapped to each alignment subclass as follows:
1175
+
1176
+ - ``l``: :class:`~.AlignLeft`
1177
+ - ``r``: :class:`~.AlignRight`
1178
+ - ``s``: :class:`~.AlignSequential`
1179
+ - ``e``: :class:`~.AlignEquispaced`
1180
+
1181
+ Note that :class:`~.AlignFunc` context is not supported because of the callback function
1182
+ stored in the context parameters.
1183
+
1184
+ .. _qpy_schedule_instructions:
1185
+
1186
+ SCHEDULE_BLOCK_INSTRUCTIONS
1187
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1188
+
1189
+ This alignment block is further followed by ``num_element`` length of block elements which may
1190
+ consist of nested schedule blocks and schedule instructions.
1191
+ Each schedule instruction starts with ``char`` representing the instruction type
1192
+ followed by the :ref:`qpy_sequence` block representing the instruction
1193
+ :attr:`~qiskit.pulse.instructions.Instruction.operands`.
1194
+ Note that the data structure of pulse :class:`~qiskit.pulse.instructions.Instruction`
1195
+ is unified so that instance can be uniquely determined by the class and a tuple of operands.
1196
+ The mapping of type char to the instruction subclass is defined as follows:
1197
+
1198
+ - ``a``: :class:`~qiskit.pulse.instructions.Acquire` instruction
1199
+ - ``p``: :class:`~qiskit.pulse.instructions.Play` instruction
1200
+ - ``d``: :class:`~qiskit.pulse.instructions.Delay` instruction
1201
+ - ``f``: :class:`~qiskit.pulse.instructions.SetFrequency` instruction
1202
+ - ``g``: :class:`~qiskit.pulse.instructions.ShiftFrequency` instruction
1203
+ - ``q``: :class:`~qiskit.pulse.instructions.SetPhase` instruction
1204
+ - ``r``: :class:`~qiskit.pulse.instructions.ShiftPhase` instruction
1205
+ - ``b``: :class:`~qiskit.pulse.instructions.RelativeBarrier` instruction
1206
+ - ``t``: :class:`~qiskit.pulse.instructions.TimeBlockade` instruction
1207
+ - ``y``: :class:`~qiskit.pulse.instructions.Reference` instruction (new in version 0.7)
1208
+
1209
+ .. _qpy_schedule_operands:
1210
+
1211
+ SCHEDULE_BLOCK_OPERANDS
1212
+ ~~~~~~~~~~~~~~~~~~~~~~~
1213
+
1214
+ The operands of these instances can be serialized through the standard QPY value serialization
1215
+ mechanism, however there are special object types that only appear in the schedule operands.
1216
+ Since the operands are serialized as :ref:`qpy_sequence`, each element must be packed with the
1217
+ INSTRUCTION_PARAM pack struct, where each payload starts with a header block consists of
1218
+ the char ``type`` and uint64_t ``size``.
1219
+ Special objects start with the following type key:
1220
+
1221
+ - ``c``: :class:`~qiskit.pulse.channels.Channel`
1222
+ - ``w``: :class:`~qiskit.pulse.library.Waveform`
1223
+ - ``s``: :class:`~qiskit.pulse.library.SymbolicPulse`
1224
+ - ``o``: string (operand string, new in version 0.7)
1225
+
1226
+ .. _qpy_schedule_channel:
1227
+
1228
+ CHANNEL
1229
+ ~~~~~~~
1230
+
1231
+ Channel block starts with channel subtype ``char`` that maps an object data to
1232
+ :class:`~qiskit.pulse.channels.Channel` subclass. Mapping is defined as follows:
1233
+
1234
+ - ``d``: :class:`~qiskit.pulse.channels.DriveChannel`
1235
+ - ``c``: :class:`~qiskit.pulse.channels.ControlChannel`
1236
+ - ``m``: :class:`~qiskit.pulse.channels.MeasureChannel`
1237
+ - ``a``: :class:`~qiskit.pulse.channels.AcquireChannel`
1238
+ - ``e``: :class:`~qiskit.pulse.channels.MemorySlot`
1239
+ - ``r``: :class:`~qiskit.pulse.channels.RegisterSlot`
1240
+
1241
+ The key is immediately followed by the channel index serialized as the INSTRUCTION_PARAM.
1242
+
1243
+ .. _qpy_schedule_waveform:
1244
+
1245
+ Waveform
1246
+ ~~~~~~~~
1247
+
1248
+ Waveform block starts with WAVEFORM header:
1249
+
1250
+ .. code-block:: c
1251
+
1252
+ struct {
1253
+ double epsilon;
1254
+ uint32_t data_size;
1255
+ _bool amp_limited;
1256
+ }
1257
+
1258
+ which is followed by ``data_size`` bytes of complex ``ndarray`` binary generated by numpy.save_.
1259
+ This represents the complex IQ data points played on a quantum device.
1260
+ :attr:`~qiskit.pulse.library.Waveform.name` is saved after the samples in the
1261
+ INSTRUCTION_PARAM pack struct, which can be string or ``None``.
1262
+
1263
+ .. _numpy.save: https://numpy.org/doc/stable/reference/generated/numpy.save.html
1264
+
1265
+ .. _qpy_schedule_symbolic_pulse:
1266
+
1267
+ SymbolicPulse
1268
+ ~~~~~~~~~~~~~
1269
+
1270
+ SymbolicPulse block starts with SYMBOLIC_PULSE header:
1271
+
1272
+ .. code-block:: c
1273
+
1274
+ struct {
1275
+ uint16_t type_size;
1276
+ uint16_t envelope_size;
1277
+ uint16_t constraints_size;
1278
+ uint16_t valid_amp_conditions_size;
1279
+ _bool amp_limited;
1280
+ }
1281
+
1282
+ which is followed by ``type_size`` utf8 bytes of :attr:`.SymbolicPulse.pulse_type` string
1283
+ that represents a class of waveform, such as "Gaussian" or "GaussianSquare".
1284
+ Then, ``envelope_size``, ``constraints_size``, ``valid_amp_conditions_size`` utf8 bytes of
1285
+ serialized symbolic expressions are generated for :attr:`.SymbolicPulse.envelope`,
1286
+ :attr:`.SymbolicPulse.constraints`, and :attr:`.SymbolicPulse.valid_amp_conditions`, respectively.
1287
+ Since string representation of these expressions are usually lengthy,
1288
+ the expression binary is generated by the python zlib_ module with data compression.
1289
+
1290
+ To uniquely specify a pulse instance, we also need to store the associated parameters,
1291
+ which consist of ``duration`` and the rest of parameters as a dictionary.
1292
+ Dictionary parameters are first dumped in the :ref:`qpy_mapping` form, and then ``duration``
1293
+ is dumped with the INSTRUCTION_PARAM pack struct.
1294
+ Lastly, :attr:`~qiskit.pulse.library.SymbolicPulse.name` is saved also with the
1295
+ INSTRUCTION_PARAM pack struct, which can be string or ``None``.
1296
+
1297
+ .. _zlib: https://docs.python.org/3/library/zlib.html
1298
+
1299
+ .. _qpy_mapping:
1300
+
1301
+ MAPPING
1302
+ ~~~~~~~
1303
+
1304
+ The MAPPING is a representation for arbitrary mapping object. This is a fixed length
1305
+ :ref:`qpy_sequence` of key-value pair represented by the MAP_ITEM payload.
1306
+
1307
+ A MAP_ITEM starts with a header defined as:
1308
+
1309
+ .. code-block:: c
1310
+
1311
+ struct {
1312
+ uint16_t key_size;
1313
+ char type;
1314
+ uint16_t size;
1315
+ }
1316
+
1317
+ which is immediately followed by the ``key_size`` utf8 bytes representing
1318
+ the dictionary key in string and ``size`` utf8 bytes of arbitrary object data of
1319
+ QPY serializable ``type``.
1320
+
1321
+ .. _qpy_circuit_calibrations:
1322
+
1323
+ CIRCUIT_CALIBRATIONS
1324
+ ~~~~~~~~~~~~~~~~~~~~
1325
+
1326
+ The CIRCUIT_CALIBRATIONS block is a dictionary to define pulse calibrations of the custom
1327
+ instruction set. This block starts with the following CALIBRATION header:
1328
+
1329
+ .. code-block:: c
1330
+
1331
+ struct {
1332
+ uint16_t num_cals;
1333
+ }
1334
+
1335
+ which is followed by the ``num_cals`` length of calibration entries, each starts with
1336
+ the CALIBRATION_DEF header:
1337
+
1338
+ .. code-block:: c
1339
+
1340
+ struct {
1341
+ uint16_t name_size;
1342
+ uint16_t num_qubits;
1343
+ uint16_t num_params;
1344
+ char type;
1345
+ }
1346
+
1347
+ The calibration definition header is then followed by ``name_size`` utf8 bytes of
1348
+ the gate name, ``num_qubits`` length of integers representing a sequence of qubits,
1349
+ and ``num_params`` length of INSTRUCTION_PARAM payload for parameters
1350
+ associated to the custom instruction.
1351
+ The ``type`` indicates the class of pulse program which is either, in principle,
1352
+ ``ScheduleBlock`` or :class:`~.Schedule`. As of QPY Version 5,
1353
+ only ``ScheduleBlock`` payload is supported.
1354
+ Finally, :ref:`qpy_schedule_block` payload is packed for each CALIBRATION_DEF entry.
1355
+
1356
+ .. _qpy_instruction_v5:
1357
+
1358
+ INSTRUCTION
1359
+ ~~~~~~~~~~~
1360
+
1361
+ The INSTRUCTION block was modified to add two new fields ``num_ctrl_qubits`` and ``ctrl_state``
1362
+ which are used to model the :attr:`.ControlledGate.num_ctrl_qubits` and
1363
+ :attr:`.ControlledGate.ctrl_state` attributes. The new payload packed struct
1364
+ format is:
1365
+
1366
+ .. code-block:: c
1367
+
1368
+ struct {
1369
+ uint16_t name_size;
1370
+ uint16_t label_size;
1371
+ uint16_t num_parameters;
1372
+ uint32_t num_qargs;
1373
+ uint32_t num_cargs;
1374
+ _Bool has_conditional;
1375
+ uint16_t conditional_reg_name_size;
1376
+ int64_t conditional_value;
1377
+ uint32_t num_ctrl_qubits;
1378
+ uint32_t ctrl_state;
1379
+ }
1380
+
1381
+ The rest of the instruction payload is the same. You can refer to
1382
+ :ref:`qpy_instructions` for the details of the full payload.
1383
+
1384
+ CUSTOM_INSTRUCTION
1385
+ ~~~~~~~~~~~~~~~~~~
1386
+
1387
+ The CUSTOM_INSTRUCTION block in QPY version 5 adds a new field
1388
+ ``base_gate_size`` which is used to define the size of the
1389
+ :class:`qiskit.circuit.Instruction` object stored in the
1390
+ :attr:`.ControlledGate.base_gate` attribute for a custom
1391
+ :class:`~.ControlledGate` object. With this change the CUSTOM_INSTRUCTION
1392
+ metadata block becomes:
1393
+
1394
+ .. code-block:: c
1395
+
1396
+ struct {
1397
+ uint16_t name_size;
1398
+ char type;
1399
+ uint32_t num_qubits;
1400
+ uint32_t num_clbits;
1401
+ _Bool custom_definition;
1402
+ uint64_t size;
1403
+ uint32_t num_ctrl_qubits;
1404
+ uint32_t ctrl_state;
1405
+ uint64_t base_gate_size
1406
+ }
1407
+
1408
+ Immediately following the CUSTOM_INSTRUCTION struct is the utf8 encoded name
1409
+ of size ``name_size``.
1410
+
1411
+ If ``custom_definition`` is ``True`` that means that the immediately following
1412
+ ``size`` bytes contains a QPY circuit data which can be used for the custom
1413
+ definition of that gate. If ``custom_definition`` is ``False`` then the
1414
+ instruction can be considered opaque (ie no definition). The ``type`` field
1415
+ determines what type of object will get created with the custom definition.
1416
+ If it's ``'g'`` it will be a :class:`~qiskit.circuit.Gate` object, ``'i'``
1417
+ it will be a :class:`~qiskit.circuit.Instruction` object.
1418
+
1419
+ Following this the next ``base_gate_size`` bytes contain the ``INSTRUCTION``
1420
+ payload for the :attr:`.ControlledGate.base_gate`.
1421
+
1422
+ Additionally an addition value for ``type`` is added ``'c'`` which is used to
1423
+ indicate the custom instruction is a custom :class:`~.ControlledGate`.
1424
+
1425
+ .. _qpy_version_4:
1426
+
1427
+ Version 4
1428
+ ---------
1429
+
1430
+ Version 4 is identical to :ref:`qpy_version_3` except that it adds 2 new type strings
1431
+ to the INSTRUCTION_PARAM struct, ``z`` to represent ``None`` (which is encoded as
1432
+ no data), ``q`` to represent a :class:`.QuantumCircuit` (which is encoded as
1433
+ a QPY circuit), ``r`` to represent a ``range`` of integers (which is encoded as
1434
+ a :ref:`qpy_range_pack`), and ``t`` to represent a ``sequence`` (which is encoded as
1435
+ defined by :ref:`qpy_sequence`). Additionally, version 4 changes the type of register
1436
+ index mapping array from ``uint32_t`` to ``int64_t``. If the values of any of the
1437
+ array elements are negative they represent a register bit that is not present in the
1438
+ circuit.
1439
+
1440
+ The :ref:`qpy_registers` header format has also been updated to
1441
+
1442
+ .. code-block:: c
1443
+
1444
+ struct {
1445
+ char type;
1446
+ _Bool standalone;
1447
+ uint32_t size;
1448
+ uint16_t name_size;
1449
+ _bool in_circuit;
1450
+ }
1451
+
1452
+ which just adds the ``in_circuit`` field which represents whether the register is
1453
+ part of the circuit or not.
1454
+
1455
+ .. _qpy_range_pack:
1456
+
1457
+ RANGE
1458
+ ~~~~~
1459
+
1460
+ A RANGE is a representation of a ``range`` object. It is defined as:
1461
+
1462
+ .. code-block:: c
1463
+
1464
+ struct {
1465
+ int64_t start;
1466
+ int64_t stop;
1467
+ int64_t step;
1468
+ }
1469
+
1470
+ .. _qpy_sequence:
1471
+
1472
+ SEQUENCE
1473
+ ~~~~~~~~
1474
+
1475
+ A SEQUENCE is a representation of an arbitrary sequence object. As sequence are just fixed length
1476
+ containers of arbitrary python objects their QPY can't fully represent any sequence,
1477
+ but as long as the contents in a sequence are other QPY serializable types for
1478
+ the INSTRUCTION_PARAM payload the ``sequence`` object can be serialized.
1479
+
1480
+ A sequence instruction parameter starts with a header defined as:
1481
+
1482
+ .. code-block:: c
1483
+
1484
+ struct {
1485
+ uint64_t size;
1486
+ }
1487
+
1488
+ followed by ``size`` elements that are INSTRUCTION_PARAM payloads, where each of
1489
+ these define an element in the sequence. The sequence object will be typecasted
1490
+ into proper type, e.g. ``tuple``, afterwards.
1491
+
1492
+ .. _qpy_version_3:
1493
+
1494
+ Version 3
1495
+ ---------
1496
+
1497
+ Version 3 of the QPY format is identical to :ref:`qpy_version_2` except that it defines
1498
+ a struct format to represent a :class:`~qiskit.circuit.library.PauliEvolutionGate`
1499
+ natively in QPY. To accomplish this the :ref:`qpy_custom_definition` struct now supports
1500
+ a new type value ``'p'`` to represent a :class:`~qiskit.circuit.library.PauliEvolutionGate`.
1501
+ Enties in the custom instructions tables have unique name generated that start with the
1502
+ string ``"###PauliEvolutionGate_"`` followed by a uuid string. This gate name is reservered
1503
+ in QPY and if you have a custom :class:`~qiskit.circuit.Instruction` object with a definition
1504
+ set and that name prefix it will error. If it's of type ``'p'`` the data payload is defined
1505
+ as follows:
1506
+
1507
+ .. _pauli_evo_qpy:
1508
+
1509
+ PAULI_EVOLUTION
1510
+ ~~~~~~~~~~~~~~~
1511
+
1512
+ This represents the high level :class:`~qiskit.circuit.library.PauliEvolutionGate`
1513
+
1514
+ .. code-block:: c
1515
+
1516
+ struct {
1517
+ uint64_t operator_count;
1518
+ _Bool standalone_op;
1519
+ char time_type;
1520
+ uint64_t time_size;
1521
+ uint64_t synthesis_size;
1522
+ }
1523
+
1524
+ This is immediately followed by ``operator_count`` elements defined by the :ref:`qpy_pauli_sum_op`
1525
+ payload. Following that we have ``time_size`` bytes representing the ``time`` attribute. If
1526
+ ``standalone_op`` is ``True`` then there must only be a single operator. The
1527
+ encoding of these bytes is determined by the value of ``time_type``. Possible values of
1528
+ ``time_type`` are ``'f'``, ``'p'``, and ``'e'``. If ``time_type`` is ``'f'`` it's a double,
1529
+ ``'p'`` defines a :class:`~qiskit.circuit.Parameter` object which is represented by a
1530
+ :ref:`qpy_param_struct`, ``e`` defines a :class:`~qiskit.circuit.ParameterExpression` object
1531
+ (that's not a :class:`~qiskit.circuit.Parameter`) which is represented by a :ref:`qpy_param_expr`.
1532
+ Following that is ``synthesis_size`` bytes which is a utf8 encoded json payload representing
1533
+ the :class:`.EvolutionSynthesis` class used by the gate.
1534
+
1535
+ .. _qpy_pauli_sum_op:
1536
+
1537
+ SPARSE_PAULI_OP_LIST_ELEM
1538
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
1539
+
1540
+ This represents an instance of :class:`.SparsePauliOp`.
1541
+
1542
+
1543
+ .. code-block:: c
1544
+
1545
+ struct {
1546
+ uint32_t pauli_op_size;
1547
+ }
1548
+
1549
+ which is immediately followed by ``pauli_op_size`` bytes which are .npy format [#f2]_
1550
+ data which represents the :class:`~qiskit.quantum_info.SparsePauliOp`.
1551
+
1552
+ Version 3 of the QPY format also defines a struct format to represent a
1553
+ :class:`~qiskit.circuit.ParameterVectorElement` as a distinct subclass from
1554
+ a :class:`~qiskit.circuit.Parameter`. This adds a new parameter type char ``'v'``
1555
+ to represent a :class:`~qiskit.circuit.ParameterVectorElement` which is now
1556
+ supported as a type string value for an INSTRUCTION_PARAM. The payload for these
1557
+ parameters are defined below as :ref:`qpy_param_vector`.
1558
+
1559
+ .. _qpy_param_vector:
1560
+
1561
+ PARAMETER_VECTOR_ELEMENT
1562
+ ~~~~~~~~~~~~~~~~~~~~~~~~
1563
+
1564
+ A PARAMETER_VECTOR_ELEMENT represents a :class:`~qiskit.circuit.ParameterVectorElement`
1565
+ object the data for a INSTRUCTION_PARAM. The contents of the PARAMETER_VECTOR_ELEMENT are
1566
+ defined as:
1567
+
1568
+ .. code-block:: c
1569
+
1570
+ struct {
1571
+ uint16_t vector_name_size;
1572
+ uint64_t vector_size;
1573
+ char uuid[16];
1574
+ uint64_t index;
1575
+ }
1576
+
1577
+ which is immediately followed by ``vector_name_size`` utf8 bytes representing
1578
+ the parameter's vector name.
1579
+
1580
+ .. _qpy_param_expr_v3:
1581
+
1582
+
1583
+ PARAMETER_EXPR
1584
+ ~~~~~~~~~~~~~~
1585
+
1586
+ Additionally, since QPY format version v3 distinguishes between a
1587
+ :class:`~qiskit.circuit.Parameter` and :class:`~qiskit.circuit.ParameterVectorElement`
1588
+ the payload for a :class:`~qiskit.circuit.ParameterExpression` needs to be updated
1589
+ to distinguish between the types. The following is the modified payload format
1590
+ which is mostly identical to the format in Version 1 and :ref:`qpy_version_2` but just
1591
+ modifies the ``map_elements`` struct to include a symbol type field.
1592
+
1593
+ A PARAMETER_EXPR represents a :class:`~qiskit.circuit.ParameterExpression`
1594
+ object that the data for an INSTRUCTION_PARAM. The contents of a PARAMETER_EXPR
1595
+ are defined as:
1596
+
1597
+ .. code-block:: c
1598
+
1599
+ struct {
1600
+ uint64_t map_elements;
1601
+ uint64_t expr_size;
1602
+ }
1603
+
1604
+ Immediately following the header is ``expr_size`` bytes of utf8 data containing
1605
+ the expression string, which is the sympy srepr of the expression for the
1606
+ parameter expression. Following that is a symbol map which contains
1607
+ ``map_elements`` elements with the format
1608
+
1609
+ .. code-block:: c
1610
+
1611
+ struct {
1612
+ char symbol_type;
1613
+ char type;
1614
+ uint64_t size;
1615
+ }
1616
+
1617
+ The ``symbol_type`` key determines the payload type of the symbol representation
1618
+ for the element. If it's ``p`` it represents a :class:`~qiskit.circuit.Parameter`
1619
+ and if it's ``v`` it represents a :class:`~qiskit.circuit.ParameterVectorElement`.
1620
+ The map element struct is immediately followed by the symbol map key payload, if
1621
+ ``symbol_type`` is ``p`` then it is followed immediately by a :ref:`qpy_param_struct`
1622
+ object (both the struct and utf8 name bytes) and if ``symbol_type`` is ``v``
1623
+ then the struct is imediately followed by :ref:`qpy_param_vector` (both the struct
1624
+ and utf8 name bytes). That is followed by ``size`` bytes for the
1625
+ data of the symbol. The data format is dependent on the value of ``type``. If
1626
+ ``type`` is ``p`` then it represents a :class:`~qiskit.circuit.Parameter` and
1627
+ size will be 0, the value will just be the same as the key. Similarly if the
1628
+ ``type`` is ``v`` then it represents a :class:`~qiskit.circuit.ParameterVectorElement`
1629
+ and size will be 0 as the value will just be the same as the key. If
1630
+ ``type`` is ``f`` then it represents a double precision float. If ``type`` is
1631
+ ``c`` it represents a double precision complex, which is represented by the
1632
+ :ref:`qpy_complex`. Finally, if type is ``i`` it represents an integer which is an
1633
+ ``int64_t``.
1634
+
1635
+ .. _qpy_version_2:
1636
+
1637
+ Version 2
1638
+ ---------
1639
+
1640
+ Version 2 of the QPY format is identical to version 1 except for the HEADER
1641
+ section is slightly different. You can refer to the :ref:`qpy_version_1` section
1642
+ for the details on the rest of the payload format.
1643
+
1644
+ HEADER
1645
+ ~~~~~~
1646
+
1647
+ The contents of HEADER are defined as a C struct are:
1648
+
1649
+ .. code-block:: c
1650
+
1651
+ struct {
1652
+ uint16_t name_size;
1653
+ char global_phase_type;
1654
+ uint16_t global_phase_size;
1655
+ uint32_t num_qubits;
1656
+ uint32_t num_clbits;
1657
+ uint64_t metadata_size;
1658
+ uint32_t num_registers;
1659
+ uint64_t num_instructions;
1660
+ }
1661
+
1662
+ This is immediately followed by ``name_size`` bytes of utf8 data for the name
1663
+ of the circuit. Following this is immediately ``global_phase_size`` bytes
1664
+ representing the global phase. The content of that data is dictated by the
1665
+ value of ``global_phase_type``. If it's ``'f'`` the data is a float and is the
1666
+ size of a ``double``. If it's ``'p'`` defines a :class:`~qiskit.circuit.Parameter`
1667
+ object which is represented by a PARAM struct (see below), ``e`` defines a
1668
+ :class:`~qiskit.circuit.ParameterExpression` object (that's not a
1669
+ :class:`~qiskit.circuit.Parameter`) which is represented by a PARAM_EXPR struct
1670
+ (see below).
1671
+
1672
+ .. _qpy_version_1:
1673
+
1674
+ Version 1
1675
+ ---------
1676
+
1677
+ HEADER
1678
+ ~~~~~~
1679
+
1680
+ The contents of HEADER as defined as a C struct are:
1681
+
1682
+ .. code-block:: c
1683
+
1684
+ struct {
1685
+ uint16_t name_size;
1686
+ double global_phase;
1687
+ uint32_t num_qubits;
1688
+ uint32_t num_clbits;
1689
+ uint64_t metadata_size;
1690
+ uint32_t num_registers;
1691
+ uint64_t num_instructions;
1692
+ }
1693
+
1694
+ This is immediately followed by ``name_size`` bytes of utf8 data for the name
1695
+ of the circuit.
1696
+
1697
+ METADATA
1698
+ ~~~~~~~~
1699
+
1700
+ The METADATA field is a UTF8 encoded JSON string. After reading the HEADER
1701
+ (which is a fixed size at the start of the QPY file) and the ``name`` string
1702
+ you then read the ``metadata_size`` number of bytes and parse the JSON to get
1703
+ the metadata for the circuit.
1704
+
1705
+ .. _qpy_registers:
1706
+
1707
+ REGISTERS
1708
+ ~~~~~~~~~
1709
+
1710
+ The contents of REGISTERS is a number of REGISTER object. If num_registers is
1711
+ > 0 then after reading METADATA you read that number of REGISTER structs defined
1712
+ as:
1713
+
1714
+ .. code-block:: c
1715
+
1716
+ struct {
1717
+ char type;
1718
+ _Bool standalone;
1719
+ uint32_t size;
1720
+ uint16_t name_size;
1721
+ }
1722
+
1723
+ ``type`` can be ``'q'`` or ``'c'``.
1724
+
1725
+ Immediately following the REGISTER struct is the utf8 encoded register name of
1726
+ size ``name_size``. After the ``name`` utf8 bytes there is then an array of
1727
+ int64_t values of size ``size`` that contains a map of the register's index to
1728
+ the circuit's qubit index. For example, array element 0's value is the index
1729
+ of the ``register[0]``'s position in the containing circuit's qubits list.
1730
+
1731
+ .. note::
1732
+
1733
+ Prior to QPY :ref:`qpy_version_4` the type of array elements was uint32_t. This was changed
1734
+ to enable negative values which represent bits in the array not present in the
1735
+ circuit
1736
+
1737
+
1738
+ The standalone boolean determines whether the register is constructed as a
1739
+ standalone register that was added to the circuit or was created from existing
1740
+ bits. A register is considered standalone if it has bits constructed solely
1741
+ as part of it, for example::
1742
+
1743
+ qr = QuantumRegister(2)
1744
+ qc = QuantumCircuit(qr)
1745
+
1746
+ the register ``qr`` would be a standalone register. While something like::
1747
+
1748
+ bits = [Qubit(), Qubit()]
1749
+ qr2 = QuantumRegister(bits=bits)
1750
+ qc = QuantumCircuit(qr2)
1751
+
1752
+ ``qr2`` would have ``standalone`` set to ``False``.
1753
+
1754
+
1755
+ .. _qpy_custom_definition:
1756
+
1757
+ CUSTOM_DEFINITIONS
1758
+ ~~~~~~~~~~~~~~~~~~
1759
+
1760
+ This section specifies custom definitions for any of the instructions in the circuit.
1761
+
1762
+ CUSTOM_DEFINITION_HEADER contents are defined as:
1763
+
1764
+ .. code-block:: c
1765
+
1766
+ struct {
1767
+ uint64_t size;
1768
+ }
1769
+
1770
+ If size is greater than 0 that means the circuit contains custom instruction(s).
1771
+ Each custom instruction is defined with a CUSTOM_INSTRUCTION block defined as:
1772
+
1773
+ .. code-block:: c
1774
+
1775
+ struct {
1776
+ uint16_t name_size;
1777
+ char type;
1778
+ uint32_t num_qubits;
1779
+ uint32_t num_clbits;
1780
+ _Bool custom_definition;
1781
+ uint64_t size;
1782
+ }
1783
+
1784
+ Immediately following the CUSTOM_INSTRUCTION struct is the utf8 encoded name
1785
+ of size ``name_size``.
1786
+
1787
+ If ``custom_definition`` is ``True`` that means that the immediately following
1788
+ ``size`` bytes contains a QPY circuit data which can be used for the custom
1789
+ definition of that gate. If ``custom_definition`` is ``False`` then the
1790
+ instruction can be considered opaque (ie no definition). The ``type`` field
1791
+ determines what type of object will get created with the custom definition.
1792
+ If it's ``'g'`` it will be a :class:`~qiskit.circuit.Gate` object, ``'i'``
1793
+ it will be a :class:`~qiskit.circuit.Instruction` object.
1794
+
1795
+ .. _qpy_instructions:
1796
+
1797
+ INSTRUCTIONS
1798
+ ~~~~~~~~~~~~
1799
+
1800
+ The contents of INSTRUCTIONS is a list of INSTRUCTION metadata objects
1801
+
1802
+ .. code-block:: c
1803
+
1804
+ struct {
1805
+ uint16_t name_size;
1806
+ uint16_t label_size;
1807
+ uint16_t num_parameters;
1808
+ uint32_t num_qargs;
1809
+ uint32_t num_cargs;
1810
+ _Bool has_conditional;
1811
+ uint16_t conditional_reg_name_size;
1812
+ int64_t conditional_value;
1813
+ }
1814
+
1815
+ This metadata object is immediately followed by ``name_size`` bytes of utf8 bytes
1816
+ for the ``name``. ``name`` here is the Qiskit class name for the Instruction
1817
+ class if it's defined in Qiskit. Otherwise it falls back to the custom
1818
+ instruction name. Following the ``name`` bytes there are ``label_size`` bytes of
1819
+ utf8 data for the label if one was set on the instruction. Following the label
1820
+ bytes if ``has_conditional`` is ``True`` then there are
1821
+ ``conditional_reg_name_size`` bytes of utf8 data for the name of the conditional
1822
+ register name. In case of single classical bit conditions the register name
1823
+ utf8 data will be prefixed with a null character "\\x00" and then a utf8 string
1824
+ integer representing the classical bit index in the circuit that the condition
1825
+ is on.
1826
+
1827
+ This is immediately followed by the INSTRUCTION_ARG structs for the list of
1828
+ arguments of that instruction. These are in the order of all quantum arguments
1829
+ (there are num_qargs of these) followed by all classical arguments (num_cargs
1830
+ of these).
1831
+
1832
+ The contents of each INSTRUCTION_ARG is:
1833
+
1834
+ .. code-block:: c
1835
+
1836
+ struct {
1837
+ char type;
1838
+ uint32_t index;
1839
+ }
1840
+
1841
+ ``type`` can be ``'q'`` or ``'c'``.
1842
+
1843
+ After all arguments for an instruction the parameters are specified with
1844
+ ``num_parameters`` INSTRUCTION_PARAM structs.
1845
+
1846
+ The contents of each INSTRUCTION_PARAM is:
1847
+
1848
+ .. code-block:: c
1849
+
1850
+ struct {
1851
+ char type;
1852
+ uint64_t size;
1853
+ }
1854
+
1855
+ After each INSTRUCTION_PARAM the next ``size`` bytes are the parameter's data.
1856
+ The ``type`` field can be ``'i'``, ``'f'``, ``'p'``, ``'e'``, ``'s'``, ``'c'``
1857
+ or ``'n'`` which dictate the format. For ``'i'`` it's an integer, ``'f'`` it's
1858
+ a double, ``'s'`` if it's a string (encoded as utf8), ``'c'`` is a complex and
1859
+ the data is represented by the struct format in the :ref:`qpy_param_expr` section.
1860
+ ``'p'`` defines a :class:`~qiskit.circuit.Parameter` object which is
1861
+ represented by a :ref:`qpy_param_struct` struct, ``e`` defines a
1862
+ :class:`~qiskit.circuit.ParameterExpression` object (that's not a
1863
+ :class:`~qiskit.circuit.Parameter`) which is represented by a :ref:`qpy_param_expr`
1864
+ struct (on QPY format :ref:`qpy_version_3` the format is tweak slightly see:
1865
+ :ref:`qpy_param_expr_v3`), ``'n'`` represents an object from numpy (either an
1866
+ ``ndarray`` or a numpy type) which means the data is .npy format [#f2]_ data,
1867
+ and in QPY :ref:`qpy_version_3` ``'v'`` represents a
1868
+ :class:`~qiskit.circuit.ParameterVectorElement` which is represented by a
1869
+ :ref:`qpy_param_vector` struct.
1870
+
1871
+ .. _qpy_param_struct:
1872
+
1873
+ PARAMETER
1874
+ ~~~~~~~~~
1875
+
1876
+ A PARAMETER represents a :class:`~qiskit.circuit.Parameter` object the data for
1877
+ a INSTRUCTION_PARAM. The contents of the PARAMETER are defined as:
1878
+
1879
+ .. code-block:: c
1880
+
1881
+ struct {
1882
+ uint16_t name_size;
1883
+ char uuid[16];
1884
+ }
1885
+
1886
+ which is immediately followed by ``name_size`` utf8 bytes representing the
1887
+ parameter name.
1888
+
1889
+ .. _qpy_param_expr:
1890
+
1891
+ PARAMETER_EXPR
1892
+ ~~~~~~~~~~~~~~
1893
+
1894
+ A PARAMETER_EXPR represents a :class:`~qiskit.circuit.ParameterExpression`
1895
+ object that the data for an INSTRUCTION_PARAM. The contents of a PARAMETER_EXPR
1896
+ are defined as:
1897
+
1898
+ The PARAMETER_EXPR data starts with a header:
1899
+
1900
+ .. code-block:: c
1901
+
1902
+ struct {
1903
+ uint64_t map_elements;
1904
+ uint64_t expr_size;
1905
+ }
1906
+
1907
+ Immediately following the header is ``expr_size`` bytes of utf8 data containing
1908
+ the expression string, which is the sympy srepr of the expression for the
1909
+ parameter expression. Follwing that is a symbol map which contains
1910
+ ``map_elements`` elements with the format
1911
+
1912
+ .. code-block:: c
1913
+
1914
+ struct {
1915
+ char type;
1916
+ uint64_t size;
1917
+ }
1918
+
1919
+ Which is followed immediately by ``PARAMETER`` object (both the struct and utf8
1920
+ name bytes) for the symbol map key. That is followed by ``size`` bytes for the
1921
+ data of the symbol. The data format is dependent on the value of ``type``. If
1922
+ ``type`` is ``p`` then it represents a :class:`~qiskit.circuit.Parameter` and
1923
+ size will be 0, the value will just be the same as the key. If
1924
+ ``type`` is ``f`` then it represents a double precision float. If ``type`` is
1925
+ ``c`` it represents a double precision complex, which is represented by :ref:`qpy_complex`.
1926
+ Finally, if type is ``i`` it represents an integer which is an ``int64_t``.
1927
+
1928
+ .. _qpy_complex:
1929
+
1930
+ COMPLEX
1931
+ ~~~~~~~
1932
+
1933
+ When representing a double precision complex value in QPY the following
1934
+ struct is used:
1935
+
1936
+
1937
+ .. code-block:: c
1938
+
1939
+ struct {
1940
+ double real;
1941
+ double imag;
1942
+ }
1943
+
1944
+ this matches the internal C representation of Python's complex type. [#f3]_
1945
+
1946
+ References
1947
+ ==========
1948
+
1949
+ .. [#f1] https://tools.ietf.org/html/rfc1700
1950
+ .. [#f2] https://numpy.org/doc/stable/reference/generated/numpy.lib.format.html
1951
+ .. [#f3] https://docs.python.org/3/c-api/complex.html#c.Py_complex
1952
+ """
1953
+
1954
+ from .exceptions import QpyError, UnsupportedFeatureForVersion, QPYLoadingDeprecatedFeatureWarning
1955
+ from .interface import dump, load
1956
+
1957
+ # For backward compatibility. Provide, Runtime, Experiment call these private functions.
1958
+ from .binary_io import (
1959
+ _write_instruction,
1960
+ _read_instruction,
1961
+ _write_parameter_expression,
1962
+ _read_parameter_expression,
1963
+ _read_parameter_expression_v3,
1964
+ )
1965
+ from .common import QPY_VERSION, QPY_COMPATIBILITY_VERSION