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
@@ -0,0 +1,1343 @@
1
+ # This code is part of Qiskit.
2
+ #
3
+ # (C) Copyright IBM 2017.
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
+ r"""
14
+ =============================================
15
+ Quantum circuit model (:mod:`qiskit.circuit`)
16
+ =============================================
17
+
18
+ .. currentmodule:: qiskit.circuit
19
+
20
+ The fundamental element of quantum computing is the *quantum circuit*. This is a computational
21
+ routine that can be run, one shot at a time, on a quantum processing unit (QPU). A circuit will act
22
+ on a predefined amount of quantum data (in Qiskit, we only directly support qubits) with unitary
23
+ operations (gates), measurements and resets. In addition, a quantum circuit can contain operations
24
+ on classical data, including real-time computations and control-flow constructs, which are executed
25
+ by the controllers of the QPU.
26
+
27
+ .. note::
28
+
29
+ You may wish to skip the introductory material and jump directly to:
30
+
31
+ * :ref:`the API overview of the whole circuit module <circuit-module-api>`
32
+ * :ref:`the detailed discussion about how circuits are represented <circuit-repr>`
33
+ * the core :class:`QuantumCircuit` class for how to build and query circuits
34
+ * :ref:`information on construction custom instructions <circuit-custom-gates>`
35
+ * :ref:`ways to work with circuit-level objects <circuit-working-with>`
36
+ * :ref:`discussion of Qiskit conventions for circuits, matrices and state labelling
37
+ <circuit-conventions>`
38
+
39
+ Circuits are at a low level of abstraction when building up quantum programs. They are the
40
+ construct that is used to build up to higher levels of abstraction, such as the :ref:`primitives of
41
+ quantum computation <qiskit-primitives>`, which accumulate data from many shots of quantum-circuit
42
+ execution, along with advanced error-mitigation techniques and measurement optimizations, into
43
+ well-typed classical data and error statistics.
44
+
45
+ In Qiskit, circuits can be defined in one of two regimes:
46
+
47
+ * an *abstract* circuit, which is defined in terms of *virtual qubits* and arbitrary high-level
48
+ operations, like encapsulated algorithms and user-defined gates.
49
+
50
+ * a *physical* circuit, which is defined in terms of the *hardware qubits* of one particular
51
+ backend, and contains only operations that this backend natively supports. You might also see
52
+ this concept referred to as an *ISA circuit*.
53
+
54
+ You convert from an abstract circuit to a physical circuit by using :ref:`Qiskit's transpilation
55
+ package <qiskit-transpiler>`, of which the top-level access point is :func:`.transpile`.
56
+
57
+ In Qiskit, a quantum circuit is represented by the :class:`QuantumCircuit` class. Below is an
58
+ example of a quantum circuit that makes a three-qubit Greenberger–Horne–Zeilinger (GHZ) state
59
+ defined as:
60
+
61
+ .. math::
62
+
63
+ |\psi\rangle = \left( |000\rangle + |111\rangle \right) / \sqrt{2}
64
+
65
+
66
+ .. plot::
67
+ :alt: Circuit diagram output by the previous code.
68
+ :include-source:
69
+
70
+ from qiskit import QuantumCircuit
71
+
72
+ # Create a circuit with a register of three qubits
73
+ circ = QuantumCircuit(3)
74
+ # H gate on qubit 0, putting this qubit in a superposition of |0> + |1>.
75
+ circ.h(0)
76
+ # A CX (CNOT) gate on control qubit 0 and target qubit 1 generating a Bell state.
77
+ circ.cx(0, 1)
78
+ # CX (CNOT) gate on control qubit 0 and target qubit 2 resulting in a GHZ state.
79
+ circ.cx(0, 2)
80
+ # Draw the circuit
81
+ circ.draw('mpl')
82
+
83
+
84
+ .. _circuit-definitions:
85
+
86
+ Circuit concepts and definitions
87
+ ================================
88
+
89
+ There is a lot of specialized terminology around quantum circuits. Much of this is common in
90
+ quantum-computing literature, while some is more specific to quantum software packages, and a small
91
+ amount specific to Qiskit. This is an alphabetical listing of some of the important concepts as a
92
+ quick reference, but does not go into detail of the foundational concepts. Consider using the `IBM
93
+ Quantum Learning platform <https://learning.quantum.ibm.com/>`_ if you want to start from the
94
+ beginning.
95
+
96
+ abstract circuit
97
+ A *circuit* defined in terms of abstract mathematical operations and *virtual qubits*. This is
98
+ typically how you think about quantum algorithms; an abstract circuit can be made up of
99
+ completely arbitrary unitary operations, measurements, and potentially *real-time classical
100
+ computation*, with no restrictions about which qubits can interact with each other.
101
+
102
+ You turn an abstract circuit into a *physical circuit* by using :ref:`Qiskit's transpilation
103
+ package <qiskit-transpiler>`.
104
+
105
+ ancilla qubit
106
+ An extra qubit that is used to help implement operations on other qubits, but whose final state
107
+ is not important for the program.
108
+
109
+ circuit
110
+ A computational routine the defines a single execution to be taken on a QPU. This can either be
111
+ an *abstract circuit* or a *physical circuit*.
112
+
113
+ clbit
114
+ A Qiskit-specific abbreviation meaning a single classical bit of data.
115
+
116
+ gate
117
+ A *unitary operation* on one or more qubits.
118
+
119
+ hardware qubit
120
+ The representation of a single qubit on a particular *QPU*. A hardware qubit has some physical
121
+ quantum-mechanical system backing it, such as superconducting circuits; unlike a *virtual
122
+ qubit*, it has particular coupling constraints and only certain gates can be applied to certain
123
+ groups of hardware qubits.
124
+
125
+ Qiskit does not distinguish *logical qubits* from any individual *physical qubits* when talking
126
+ about hardware qubits. A QPU may implement its hardware qubits as logical qubits, where each
127
+ hardware qubit comprises many physical qubits that are controlled and error-corrected opaquely
128
+ to Qiskit by the control electronics. More likely, for near-term applications, a QPU will be
129
+ directly exposing its physical qubits as the hardware qubits for Qiskit to reason about.
130
+
131
+ Both physical and logical qubits will have coupling constraints between them, only permit
132
+ certain quantum operations on them, and have scheduling concerns between them. Qiskit abstracts
133
+ these concerns together in the concept of hardware qubits. In the early days of quantum error
134
+ correction, particular backends may let you access their qubit resources either as high-level
135
+ logical qubits or as low-level physical qubits through Qiskit.
136
+
137
+ instruction set architecture (ISA)
138
+ The abstract model of which operations are available on which sets of *hardware qubits* on one
139
+ particular *QPU*. For example, one QPU may allow :math:`\sqrt X` and :math:`R_Z` operations on
140
+ all single hardware qubits, and :math:`CX` operations on certain pairs of hardware qubits.
141
+
142
+ logical qubit
143
+ A collection of several *physical qubits* that are controlled together by a QPU (from the user's
144
+ perspective) to apply real-time quantum error correction. A logical qubit is a type of
145
+ *hardware qubit* for Qiskit.
146
+
147
+ measurement
148
+ The act of extracting one classical bit of a data from a single qubit state. This is an
149
+ irreversible operation, and usually destroys entanglement and phase coherence between the target
150
+ qubit and the rest of the system.
151
+
152
+ physical circuit
153
+ A *circuit* defined in terms of *hardware qubits* and only the quantum operations available in a
154
+ particular *QPU's* *ISA*. Physical circuits are tied to one particular QPU architecture, and
155
+ will not run on other incompatible architectures. You may also hear this referred to as an *ISA
156
+ circuit*.
157
+
158
+ You typically get a physical circuit by using :ref:`Qiskit's transpilation routines
159
+ <qiskit-transpiler>` on an *abstract circuit* that you constructed.
160
+
161
+ physical qubit
162
+ A controllable two-level quantum system. This is literally one "physics" qubit, such as a
163
+ transmon or the electronic state of a trapped ion. A QPU may expose this directly as its
164
+ *hardware qubit*, or combine several physical qubits into a *logical qubit*.
165
+
166
+ quantum processing unit (QPU)
167
+ Analogous to a CPU in classical computing or a GPU in graphics processing, a QPU is the hardware
168
+ that runs quantum operations on quantum data. You can always expect a QPU that uses the
169
+ *circuit* model of computation to be able to perform some set of *gates*, and *measurement*
170
+ operations. Depending on the particular technology, they also may be able to run some real-time
171
+ classical computations as well, such as classical control flow and bitwise calculations on
172
+ classical data.
173
+
174
+ qubit
175
+ The basic unit of quantum information.
176
+
177
+ real-time classical computation
178
+ Any classical computation that can happen within the execution of a single shot of a *circuit*,
179
+ where the results of the classical computation can affect later execution of the circuit. The
180
+ amount of real-time classical computation available with particular *QPU*\ s will vary
181
+ significantly dependent on many factors, such as the controlling electronics and the qubit
182
+ technology in use. You should consult your hardware vendor's documentation for more information
183
+ on this.
184
+
185
+ unitary operation
186
+ A reversible operation on a quantum state. All quantum *gates* are unitary operations (by
187
+ definition).
188
+
189
+ virtual qubit
190
+ An abstract, mathematical *qubit* used to build an *abstract circuit*. Virtual qubits are how
191
+ one typically thinks about quantum algorithms at a high level; we assume that all quantum gates
192
+ are valid on all virtual qubits, and all virtual qubits are always connected to every other
193
+ virtual qubit.
194
+
195
+ When mapping to hardware, virtual qubits must be assigned to *hardware qubits*. This mapping
196
+ need not be one-to-one. Typically, one virtual qubit will need to be swapped from one hardware
197
+ qubit to another over the course of a circuit execution in order to satisfy coupling constraints
198
+ of the underlying QPU. It is not strictly necessary for all virtual qubits used in a circuit to
199
+ be mapped to a physical qubit at any given point in a *physical circuit*; it could be that a
200
+ virtual qubit is measured (collapsing its state) and then never used again, so a new virtual
201
+ qubit could take its place. Evaluating these conditions to map a virtual circuit to a physical
202
+ circuit is the job of :ref:`Qiskit's transpilation package <qiskit-transpiler>`.
203
+
204
+
205
+ .. _circuit-module-api:
206
+
207
+ API overview of qiskit.circuit
208
+ ==============================
209
+
210
+ All objects here are described in more detail, and in their greater context in the following
211
+ sections. This section provides an overview of the API elements documented here.
212
+
213
+ ..
214
+ TODO: actually write the "in-depth section on building circuits and cross-ref to it.
215
+
216
+ The principal class is :class:`.QuantumCircuit`, which has its own documentation page, including
217
+ an in-depth section on building circuits. Quantum data and the simplest classical data are
218
+ represented by "bits" and "registers":
219
+
220
+ * :class:`Bit`, an atom of data
221
+ * :class:`Qubit`
222
+ * :class:`Clbit`
223
+ * :class:`AncillaQubit`
224
+
225
+ * :class:`Register`, a collection of bits
226
+ * :class:`QuantumRegister`
227
+ * :class:`ClassicalRegister`
228
+ * :class:`AncillaRegister`
229
+
230
+ Within a circuit, each complete :class:`CircuitInstruction` is made up of an :class:`Operation`
231
+ (which might be an :class:`Instruction`, a :class:`Gate`, or some other subclass) and the qubit
232
+ and clbit operands. The core base classes here are:
233
+
234
+ * :class:`CircuitInstruction`, an operation and its operands
235
+ * :class:`InstructionSet`, a temporary handle to a slice of circuit data
236
+ * :class:`Operation`, any abstract mathematical object or hardware instruction
237
+ * :class:`AnnotatedOperation`, a subclass with applied abstract modifiers
238
+ * :class:`InverseModifier`
239
+ * :class:`ControlModifier`
240
+ * :class:`PowerModifier`
241
+
242
+ The most common concrete subclass of the minimal, abstract :class:`Operation` interface is the
243
+ :class:`Instruction`. While :class:`Operation` can include abstract mathematical objects,
244
+ an :class:`Instruction` is something that could conceivably run directly on hardware. This is in
245
+ turn subclassed by :class:`Gate` and :class:`ControlledGate` that further add unitarity and
246
+ controlled semantics on top:
247
+
248
+ * :class:`Instruction`, representing a hardware-based instruction
249
+ * :class:`Gate`, representing a hardware instruction that is unitary
250
+ * :class:`ControlledGate`, representing a gate with control structure.
251
+
252
+ Qiskit includes a large library of standard gates and circuits, which is documented in
253
+ :mod:`qiskit.circuit.library`. Many of these are declared as Python-object singletons. The
254
+ machinery for this is described in detail in :mod:`qiskit.circuit.singleton`, of which the main
255
+ classes are each a singleton form of the standard instruction–gate hierarchy:
256
+
257
+ * :class:`~singleton.SingletonInstruction`
258
+ * :class:`~singleton.SingletonGate`
259
+ * :class:`~singleton.SingletonControlledGate`
260
+
261
+ Some instructions are particularly special in that they affect the control flow or data flow of the
262
+ circuit. The top-level ones are:
263
+
264
+ * :class:`Barrier`, to mark parts of the circuit that should be optimized independently
265
+ * :class:`Delay`, to insert a real-time wait period
266
+ * :class:`Measure`, to measure a :class:`Qubit` into a :class:`Clbit`
267
+ * :class:`Reset`, to irreversibly reset a qubit to the :math:`\lvert0\rangle` state
268
+ * :class:`Store`, to write a real-time classical expression to a storage location
269
+ * :class:`ControlFlowOp`, which has specific subclasses:
270
+ * :class:`BreakLoopOp`, to break out of the nearest containing loop
271
+ * :class:`ContinueLoopOp`, to move immediately to the next iteration of the containing loop
272
+ * :class:`ForLoopOp`, to loop over a fixed range of values
273
+ * :class:`IfElseOp`, to conditionally enter one of two subcircuits
274
+ * :class:`SwitchCaseOp`, to conditionally enter one of many subcircuits
275
+ * :class:`WhileLoopOp`, to repeat a subcircuit until a condition is falsified
276
+ * :class:`BoxOp`, to group a series of instructions for later processing
277
+
278
+ :ref:`Circuits can include classical expressions that are evaluated in real time
279
+ <circuit-repr-real-time-classical>`, while the QPU is executing a single shot of the circuit. These
280
+ are primarily documented in the module documentation of :mod:`qiskit.circuit.classical`. You might
281
+ be particularly interested in the base classes (which are not exposed from the :mod:`qiskit.circuit`
282
+ root):
283
+
284
+ * :class:`~classical.expr.Var`, a typed classical storage location in a circuit
285
+ * :class:`~classical.expr.Expr`, a real-time-evaluated expression
286
+ * :class:`~classical.types.Type`, the classical type of an expression.
287
+
288
+ In addition to this real-time expression evaluation, which is limited by classical hardware
289
+ representations of data, Qiskit has the concept of "compile-time" parametrization, which is done in
290
+ abstract symbolic algebra. These are typically used to represent gate angles in high-level
291
+ algorithms that might want to perform numerical derivatives, but they are an older part of Qiskit
292
+ than the real-time evaluation, so are still used in some places to do general parametrization. The
293
+ main related classes are:
294
+
295
+ * :class:`Parameter`, the atom of compile-time expressions
296
+ * :class:`ParameterExpression`, a symbolic calculation on parameters
297
+ * :class:`ParameterVector`, a convenience collection of many :class:`Parameter`\ s
298
+ * :class:`ParameterVectorElement`, a subclass of :class:`Parameter` used by :class:`ParameterVector`
299
+
300
+ The :mod:`qiskit.circuit` module also exposes some calculation classes that work with circuits to
301
+ assist compilation workflows. These include:
302
+
303
+ * :class:`EquivalenceLibrary`, a database of decomposition relations between gates and circuits
304
+ * :data:`SessionEquivalenceLibrary`, a mutable instance of :class:`EquivalenceLibrary` which is used
305
+ by default by the compiler's :class:`.BasisTranslator`.
306
+
307
+ There are also utilities for generating random circuits:
308
+
309
+ * :func:`random.random_circuit`
310
+ * :func:`random.random_clifford_circuit`
311
+
312
+ Finally, the circuit module has its own exception class, to indicate when things went wrong in
313
+ circuit-specific manners:
314
+
315
+ * :exc:`CircuitError`
316
+
317
+
318
+ .. _circuit-repr:
319
+
320
+ Representation of circuits in Qiskit
321
+ ====================================
322
+
323
+ The main user-facing class for representing circuits is :class:`QuantumCircuit`. This can be either
324
+ an abstract circuit or a physical circuit. There is much more information about the
325
+ :class:`QuantumCircuit` class itself and the multitude of available methods on it in its class
326
+ documentation.
327
+
328
+ Internally, a :class:`QuantumCircuit` contains the qubits, classical bits, compile-time parameters,
329
+ real-time variables, and other tracking information about the data it acts on and how it is
330
+ parametrized. It then contains a sequence of :class:`CircuitInstruction`\ s, which contain
331
+ the particular operation (gate, measurement, etc) and its operands (the qubits and classical bits).
332
+
333
+
334
+ Bits and registers
335
+ ------------------
336
+
337
+ Qubits and classical bits are represented by a shared base :class:`Bit` type, which is just intended
338
+ to be a "type tag"; the classes have no behavior other than being immutable objects:
339
+
340
+ .. autoclass:: Bit
341
+ .. autoclass:: Qubit
342
+ :show-inheritance:
343
+ :class-doc-from: class
344
+ .. autoclass:: Clbit
345
+ :show-inheritance:
346
+ :class-doc-from: class
347
+
348
+ Qubits and clbits are instantiated by users with no arguments, such as by ``Qubit()``. Bits compare
349
+ equal if they are the same Python object, or if they were both created by a register of the same
350
+ name and size, and they refer to the same index within that register. There is also a special type
351
+ tag for "ancilla" qubits, but this is little used in the current state
352
+ of Qiskit:
353
+
354
+ .. autoclass:: AncillaQubit
355
+ :show-inheritance:
356
+ :class-doc-from: class
357
+
358
+ A collection bits of the same type can be encapsulated in a register of the matching type. The base
359
+ functionality is in a base class that is not directly instantiated:
360
+
361
+ .. autoclass:: Register
362
+ :members:
363
+
364
+ Each of the defined bit subtypes has an associated register, which have the same constructor
365
+ signatures, methods and properties as the base class:
366
+
367
+ .. autoclass:: QuantumRegister
368
+ :show-inheritance:
369
+ :class-doc-from: class
370
+ .. autoclass:: ClassicalRegister
371
+ :show-inheritance:
372
+ :class-doc-from: class
373
+ .. autoclass:: AncillaRegister
374
+ :show-inheritance:
375
+ :class-doc-from: class
376
+
377
+ A common way to instantiate several bits at once is to create a register, such as by
378
+ ``QuantumRegister("my_qreg", 5)``. This has the advantage that you can give that collection of bits
379
+ a name, which will appear during circuit visualizations (:meth:`QuantumCircuit.draw`) and exports to
380
+ interchange languages (see :mod:`.qasm2` and :mod:`.qasm3`). You can also pass a name and a list of
381
+ pre-constructed bits, but this creates an "aliasing register", which are very poorly supported on
382
+ hardware.
383
+
384
+ Circuits track registers, but registers themselves impart almost no behavioral differences on
385
+ circuits. The only exception is that :class:`ClassicalRegister`\ s can be implicitly cast to
386
+ unsigned integers for use in conditional comparisons of :ref:`control flow operations
387
+ <circuit-control-flow-repr>`.
388
+
389
+ Classical registers and bits were the original way of representing classical data in Qiskit, and
390
+ remain the most supported currently. Longer term, the data model is moving towards a more complete
391
+ and strongly typed representation of a range of classical data (see
392
+ :ref:`circuit-repr-real-time-classical`), but you will still very commonly use classical bits in
393
+ current Qiskit.
394
+
395
+
396
+ Instruction contexts
397
+ --------------------
398
+
399
+ The scalar type of the :attr:`QuantumCircuit.data` sequence is the "instruction context" object,
400
+ :class:`CircuitInstruction`. This is essentially just a data class that contains a representation
401
+ of what is to be done (its :attr:`~CircuitInstruction.operation`), and the data it acts on (the
402
+ :attr:`~CircuitInstruction.qubits` and :attr:`~CircuitInstruction.clbits`).
403
+
404
+ .. autosummary::
405
+ :toctree: ../stubs/
406
+
407
+ CircuitInstruction
408
+
409
+ Programmatically, this class is actually implemented in Rust and is a constructed handle to internal
410
+ data within Rust space. Mutations to instances of this class will not be reflected in the circuit.
411
+ In general, you cannot mutate instruction contexts that are already in the circuit directly; the
412
+ :class:`QuantumCircuit` interface is designed for storing and building circuits, while the
413
+ :ref:`transpiler and its passes <qiskit-transpiler>`, and its intermediate :class:`.DAGCircuit`
414
+ representation, are where you should look for an interface to mutate circuits.
415
+
416
+ The :class:`QuantumCircuit` methods that add instructions to circuits (such as
417
+ :meth:`~QuantumCircuit.append`, and all the helper standard-gate methods) return an
418
+ :class:`InstructionSet`, which is a handle to several :class:`CircuitInstruction`\ s simultaneously.
419
+
420
+ .. autosummary::
421
+ :toctree: ../stubs/
422
+
423
+ InstructionSet
424
+
425
+ This :class:`InstructionSet` is now little used in Qiskit. It provides a very minimal set of
426
+ methods to perform post-append mutations on instructions (which *will* be propagated to the
427
+ circuit), but these are now discouraged and you should use the alternatives noted in those methods.
428
+
429
+
430
+ .. _circuit-operations-instructions:
431
+
432
+ Operations, instructions and gates
433
+ ----------------------------------
434
+
435
+ Within a :class:`CircuitInstruction`, the minimal interface that any operation must fulfill is
436
+ :class:`Operation`. This is a *very* high level view, and only usable for abstract circuits. The
437
+ main purpose of treating operations as :class:`Operation` is to allow arbitrary mathematical
438
+ objects (such as :class:`.quantum_info.Operator`) to be added to abstract circuits directly.
439
+
440
+ .. autosummary::
441
+ :toctree: ../stubs/
442
+
443
+ Operation
444
+
445
+ Most operations, including all operations on physical circuits, are instances of the more concretely
446
+ defined :class:`Instruction`. This represents any instruction that some QPU might be able to carry
447
+ out natively, such as :class:`Measure`. :class:`Instruction` need not be unitary (much as
448
+ :class:`Measure` isn't); an instruction is specifically unitary if it is a :class:`Gate`.
449
+
450
+ .. autosummary::
451
+ :toctree: ../stubs/
452
+
453
+ Instruction
454
+
455
+ :class:`Instruction`\ s can be near arbitrary, provided they only act on :class:`Qubit`\ s and
456
+ :class:`Clbit`\ s, and are parametrized by their :attr:`~Instruction.params`; they should not
457
+ attempt to "close over" outer circuit registers, or use hidden parameters inside themselves.
458
+ :class:`Instruction`\ s can be related to other circuits to provide a decompositions by using
459
+ their :attr:`Instruction.definition` attribute, which provides a local, one-off decomposition. This
460
+ can be in whatever basis set of operations is most convenient to you, as long as the definitions of
461
+ all contained gates have some topological order; that is, you cannot use a gate in a definition if
462
+ its own definition depends on the parent. If the :class:`Instruction` should be considered entirely
463
+ opaque to optimizers, its :attr:`~Instruction.definition` can be ``None``. See
464
+ :ref:`circuit-custom-gates` for more detail.
465
+
466
+ The :attr:`~Instruction.params` of an instruction can technically be arbitrary, but in general you
467
+ should attempt to stick to parametrizations in terms of real numbers, wherever possible. Qiskit
468
+ itself breaks this rule in many places, and you will find all sorts of unusual types in
469
+ :attr:`Instruction.params` fields, but these are an annoying source of bugs because they often imply
470
+ the need for type-aware special casing. If your instruction is parametrized in terms of angles, you
471
+ will be able to reliably use :ref:`compile-time parametrization in it
472
+ <circuit-compile-time-parameters>`, and it will integrate well with
473
+ :meth:`QuantumCircuit.assign_parameters`.
474
+
475
+ While :class:`Instruction` is not necessarily unitary, its subclass :class:`Gate` implies unitarity,
476
+ and adds :meth:`~Gate.to_matrix` and :meth:`~Gate.control` methods to all the methods inherited from
477
+ :class:`Instruction`.
478
+
479
+ .. autosummary::
480
+ :toctree: ../stubs/
481
+
482
+ Gate
483
+
484
+ :class:`Gate` inherits all the methods for :class:`Instruction` and all the same considerations
485
+ about its :attr:`~Instruction.params` and :attr:`~Instruction.definition` field, except of course
486
+ that :class:`Gate`\ s cannot act on any classical resources.
487
+
488
+ :class:`Gate` instances can (and should) have a base :attr:`~Instruction.definition`, but you can
489
+ also specify several different decompositions in different bases by using an
490
+ :class:`EquivalenceLibrary`.
491
+
492
+ Subclassing :class:`Gate`, Qiskit has a special :class:`ControlledGate` class as well. This class
493
+ is the base of many standard-library gates that are controlled (such as :class:`CXGate`), which is
494
+ where you are most likely to encounter it:
495
+
496
+ .. autosummary::
497
+ :toctree: ../stubs/
498
+
499
+ ControlledGate
500
+
501
+ Each of :class:`Instruction`, :class:`Gate` and :class:`ControlledGate` has a corresponding
502
+ singleton type, built using the machinery described in :mod:`qiskit.circuit.singleton`. The
503
+ module-level documentation contains full details, along with descriptions of
504
+ :class:`.SingletonInstruction`, :class:`.SingletonGate` and :class:`.SingletonControlledGate`. From
505
+ a user's perspective, little changes based on whether the base class is a singleton or not; the
506
+ intention always remains that you should call :meth:`~Instruction.to_mutable` first if you need to
507
+ get a safe-to-mutate owned copy of an instruction (you cannot assume that an arbitrary instruction
508
+ is mutable), and while direct :class:`type` inspection is discouraged, if you do need it, the
509
+ reliable way to find the "base" type of a potentially singleton instruction is to use
510
+ :attr:`~Instruction.base_class`.
511
+
512
+ :class:`ControlledGate` uses the same mechanisms as :ref:`subclassing gates <circuit-custom-gates>`
513
+ to define a fixed, lazy synthesis for itself. This is naturally not hardware-aware, and harder to
514
+ hook into the synthesis routines of the compiler, but works better as a concrete
515
+ :class:`Instruction` that could potentially be run natively on hardware. For cases where synthesis
516
+ and abstract optimization is more important, Qiskit offers a composable class called
517
+ :class:`AnnotatedOperation`, which tracks "gate modifiers" (of which :class:`ControlModifier` is
518
+ one) to apply to the inner :attr:`~AnnotatedOperation.base_op`.
519
+
520
+ .. autosummary::
521
+ :toctree: ../stubs/
522
+
523
+ AnnotatedOperation
524
+
525
+ The available modifiers for :class:`AnnotatedOperation` are:
526
+
527
+ .. autoclass:: InverseModifier
528
+ .. autoclass:: ControlModifier
529
+ .. autoclass:: PowerModifier
530
+
531
+ For information on how to create custom gates and instructions, including how to build one-off
532
+ objects, and re-usable parametric gates via subclassing, see :ref:`circuit-custom-gates` below.
533
+ The Qiskit circuit library in :mod:`qiskit.circuit.library` contains many predefined gates and
534
+ circuits for you to use.
535
+
536
+
537
+ Built-in special instructions
538
+ -----------------------------
539
+
540
+ Qiskit contains a few :class:`Instruction` classes that are in some ways "special". These typically
541
+ have special handling in circuit code, in the transpiler, or the models of hardware. These are all
542
+ generally instructions you might already be familiar with.
543
+
544
+ Measurements in Qiskit are of a single :class:`Qubit` into a single :class:`Clbit`. These are the
545
+ two that the instruction is applied to. Measurements are in the computational basis.
546
+
547
+ .. autoclass:: Measure(label=None)
548
+ :show-inheritance:
549
+
550
+ Related to measurements, there is a :class:`Reset` operation, which produces no classical data but
551
+ instructs hardware to return the qubit to the :math:`\lvert0\rangle` state. This is assumed to
552
+ happen incoherently and to collapse any entanglement.
553
+
554
+ .. autoclass:: Reset(label=None)
555
+ :show-inheritance:
556
+
557
+ Hardware can be instructed to apply a real-time idle period on a given qubit. A scheduled circuit
558
+ (see :mod:`qiskit.transpiler`) will include all the idle times on qubits explicitly in terms of this
559
+ :class:`Delay`.
560
+
561
+ .. autoclass:: Delay
562
+ :show-inheritance:
563
+ .. autoclass:: Duration
564
+
565
+
566
+ The :class:`Barrier` instruction can span an arbitrary number of qubits and clbits, and is a no-op
567
+ in hardware. During transpilation and optimization, however, it blocks any optimizations from
568
+ "crossing" the barrier; that is, in:
569
+
570
+ .. plot::
571
+ :include-source:
572
+ :nofigs:
573
+
574
+ from qiskit.circuit import QuantumCircuit
575
+
576
+ qc = QuantumCircuit(1)
577
+ qc.x(0)
578
+ qc.barrier()
579
+ qc.x(0)
580
+
581
+ it is forbidden for the optimizer to cancel out the two :math:`X` instructions.
582
+
583
+ .. autoclass:: Barrier
584
+ :show-inheritance:
585
+
586
+ The :class:`Store` instruction is particularly special, in that it allows writing the result of a
587
+ :ref:`real-time classical computation expression <circuit-repr-real-time-classical>` (an
588
+ :class:`.expr.Expr`) in a local classical variable (a :class:`.expr.Var`). It takes *neither*
589
+ :class:`Qubit` nor :class:`Clbit` operands, but has an explicit :attr:`~Store.lvalue` and
590
+ :attr:`~Store.rvalue`.
591
+
592
+ For example, to determine the parity of a bitstring ``cr`` and store it in another register ``creg``,
593
+ the :class:`Store` instruction can be used in the following way::
594
+
595
+ parity = expr.lift(cr[0])
596
+ for i in range(1,n):
597
+ parity = expr.bit_xor(cr[i], parity)
598
+ qc.store(creg[0], parity)
599
+
600
+
601
+
602
+ .. autoclass:: Store
603
+ :show-inheritance:
604
+ :members:
605
+ :no-inherited-members:
606
+
607
+
608
+ .. _circuit-repr-real-time-classical:
609
+
610
+ Real-time classical computation
611
+ -------------------------------
612
+
613
+ .. seealso::
614
+ :mod:`qiskit.circuit.classical`
615
+ Module-level documentation for how the variable-, expression- and type-systems work, the
616
+ objects used to represent them, and the classical operations available.
617
+
618
+ :ref:`circuit-real-time-methods`
619
+ The :class:`QuantumCircuit` methods for working with these variables in the context of a
620
+ single circuit.
621
+
622
+ Qiskit has rudimentary low-level support for representing real-time classical computations, which
623
+ happen during the QPU execution and affect the results. We are still relatively early into hardware
624
+ support for these concepts as well, so beware that you will need to work closely with your hardware
625
+ provider's documentation to get the best use out of any real-time classical computation.
626
+
627
+ These real-time calculations are represented by the expression and type system in
628
+ :mod:`qiskit.circuit.classical`. At a high level, all real-time expressions are represented by an
629
+ :class:`.Expr` node, which is part of an expression "tree" representation, which has a well-defined
630
+ :class:`~.classical.Type` associated with it at every level. See the module-level documentation for
631
+ much more detail on the internal representations of these classes.
632
+
633
+ The result of a real-time :class:`.Expr` can be used directly in certain places. Currently this is
634
+ limited to conditions of :class:`.IfElseOp` and :class:`.WhileLoopOp`, and the target of
635
+ :class:`.SwitchCaseOp`. The result can also be stored in a typed classical storage location, using
636
+ the :class:`.Store` instruction (or its :meth:`QuantumCircuit.store` constructor), backed by a
637
+ :class:`.expr.Var` node.
638
+
639
+ A circuit can contain manual classical storage locations, represented internally by the
640
+ :class:`~.expr.Var` node of the :class:`.Expr` tree. These have an attached classical type (like
641
+ any other expression). These can either be declared and initialized within each execution of the
642
+ circuit (:meth:`~QuantumCircuit.add_var`), or be inputs to the circuit
643
+ (:meth:`~QuantumCircuit.add_input`).
644
+
645
+ .. _circuit-compile-time-parameters:
646
+
647
+ Compile-time parametrization
648
+ ----------------------------
649
+
650
+ Various parametric :class:`Instruction` instances in Qiskit can be parametrized in ways that are
651
+ designed to be resolved at compile time. These are characterized by the use of the
652
+ :class:`Parameter` and :class:`ParameterExpression` classes.
653
+
654
+ .. autosummary::
655
+ :toctree: ../stubs/
656
+
657
+ Parameter
658
+ ParameterExpression
659
+
660
+ The main way that this differs from the :class:`expr.Var` variables used in real-time classical
661
+ computation is that :class:`ParameterExpression` is a symbolic representation of a mathematical
662
+ expression. The semantics of the expression are those of regular mathematics over the continuous
663
+ real numbers (and, in limited cases, over the complex numbers). In contrast, :class:`.Var` is a
664
+ handle to a variable stored on a classical computer, such as a floating-point value or an
665
+ fixed-width integer, which are always discrete.
666
+
667
+ In other words, you can expect :class:`ParameterExpression` to do symbolic simplifications that are
668
+ valid in mathematics, such as simplifying :math:`(x + y - x) / y \to 1`. Such a simplification is
669
+ not valid in floating-point arithmetic, and :class:`.expr.Expr` will not do this.
670
+
671
+ The "compile-time" part of these parameters means that you typically will want to "assign" values to
672
+ the parameters before sending the circuit for execution. These parameters can typically be used
673
+ anywhere that expects a mathematical angle (like a rotation gate's parameters), with the caveat that
674
+ hardware will usually require them to be assigned to a proper classically typed value before
675
+ execution. You can do this assignment using :meth:`QuantumCircuit.assign_parameters`.
676
+
677
+ You may want to use many parameters that are related to each other. To make this easier (and to
678
+ avoid you needing to come up with many names), you can use the convenience constructor
679
+ :class:`ParameterVector`. The elements of the vector are all valid :class:`Parameter` instances, of
680
+ a special subclass :class:`ParameterVectorElement`.
681
+
682
+ .. autosummary::
683
+ :toctree: ../stubs/
684
+
685
+ ParameterVector
686
+ ParameterVectorElement
687
+
688
+ .. _circuit-control-flow-repr:
689
+
690
+ Control flow in circuits
691
+ ------------------------
692
+
693
+ Within :class:`QuantumCircuit`, classical control flow is represented by specific
694
+ :class:`Instruction`\ s, which are subclasses of :class:`ControlFlowOp`.
695
+
696
+ .. autosummary::
697
+ :toctree: ../stubs/
698
+
699
+ ControlFlowOp
700
+
701
+ For convenience, there is a :class:`frozenset` instance containing the :attr:`.Instruction.name`
702
+ attributes of each of the control-flow operations.
703
+
704
+ .. data:: CONTROL_FLOW_OP_NAMES
705
+
706
+ Set of the instruction names of Qiskit's known control-flow operations.
707
+
708
+ The :func:`.get_control_flow_name_mapping` function allows to access the control-flow operation
709
+ classes associated to each name.
710
+
711
+ .. autofunction:: get_control_flow_name_mapping
712
+
713
+ These control-flow operations (:class:`IfElseOp`, :class:`WhileLoopOp`,
714
+ :class:`SwitchCaseOp`, :class:`ForLoopOp` and :class:`BoxOp`) all have specific state that defines
715
+ the branching conditions and strategies, but contain all the different subcircuit blocks that
716
+ might be entered in their :attr:`~ControlFlowOp.blocks` property.
717
+
718
+ .. autosummary::
719
+ :toctree: ../stubs/
720
+
721
+ IfElseOp
722
+ WhileLoopOp
723
+ SwitchCaseOp
724
+ ForLoopOp
725
+ BoxOp
726
+
727
+ The :class:`.SwitchCaseOp` also understands a special value:
728
+
729
+ .. autodata:: CASE_DEFAULT
730
+
731
+ In addition to the block-structure control-flow operations, there are also two special instructions
732
+ that affect the flow of control when within loops. These correspond to typical uses of the
733
+ ``break`` and ``continue`` statements in classical programming languages.
734
+
735
+ .. autosummary::
736
+ :toctree: ../stubs/
737
+
738
+ BreakLoopOp
739
+ ContinueLoopOp
740
+
741
+ .. note::
742
+ The classes representations are documented here, but please note that manually constructing
743
+ these classes is a low-level operation that we do not expect users to need to do frequently.
744
+
745
+ Users should read :ref:`circuit-control-flow-methods` for the recommended workflows for building
746
+ control-flow-enabled circuits.
747
+
748
+ Since :class:`ControlFlowOp` subclasses are also :class:`Instruction` subclasses, this means that
749
+ the way they are stored in :class:`CircuitInstruction` instances has them "applied" to a sequence of
750
+ qubits and clbits in its :attr:`~CircuitInstruction.qubits` and :attr:`~CircuitInstruction.clbits`
751
+ attributes. This can lead to subtle data-coherence problems: the :class:`Qubit` and :class:`Clbit`
752
+ objects used inside the subcircuit blocks of the control-flow ops will not necessarily be identical
753
+ to the corresponding objects in the :class:`CircuitInstruction`. Any code that consumes
754
+ control-flow operations in Qiskit needs to be aware of this; within a subcircuit, you should treat
755
+ ``subcircuit.qubits[i]`` as if it were really ``outer_instruction.qubits[i]``, and so on. You can
756
+ generate an easy lookup table for this by doing::
757
+
758
+ cf_instruction: CircuitInstruction = ...
759
+ cf_operation: ControlFlowOp = cf_instruction.operation
760
+ for block in blocks:
761
+ # Mappings of "inner" qubits/clbits to the outer ones.
762
+ qubit_map = dict(zip(block.qubits, cf_instruction.qubits))
763
+ clbit_map = dict(zip(block.clbits, cf_instruction.clbits))
764
+
765
+ # ... do something with `block` ...
766
+
767
+ Remember that you will need to propagate this information if you recurse into subblocks of
768
+ control-flow operations.
769
+
770
+ ..
771
+ TODO: insert cross-ref to control-flow builder guide into below paragraph once written.
772
+
773
+ All the subcircuit blocks in a :class:`ControlFlowOp` are required to contain the same numbers of
774
+ :class:`Qubit`\ s and :class:`Clbit`\ s, referring to the same outer bits in the same order, such
775
+ that the :class:`zip` loop given in the code block above works. The inner-circuit :class:`Bit`
776
+ objects do not need to be literally the same objects. When using the control-flow builder interface
777
+ (which, it cannot be stressed enough, is *highly* recommended for users), the builders will arrange
778
+ that the inner bit objects *are* identical to the outer bit objects; the ``qubit_map`` in the code
779
+ block above will always be a mapping ``{x: x}``, but if you are consuming the blocks, you should be
780
+ prepared for the case that the mapping is required.
781
+
782
+ Any :class:`ClassicalRegister`\ s used in a control-flow subcircuit must also be present in all
783
+ containing blocks (*i.e.* any containing control-flow operations, and the outermost circuit), and
784
+ all blocks in the same :class:`ControlFlowOp` need to contain the same registers. Again, the
785
+ builder interface will arrange for this to be the case (or produce an eager error if they cannot).
786
+
787
+ When the low-level construction is being used the inner :class:`QuantumCircuit` blocks must
788
+ manually close over any outer-scope :ref:`real-time classical computation variables
789
+ <circuit-repr-real-time-classical>` that they use. This is marked by these being in the
790
+ :meth:`~QuantumCircuit.iter_captured_vars` iterator for that block. Libraries constructing these
791
+ blocks manually will need to track these captures when building control-flow circuit blocks and add
792
+ them to the block using :meth:`~QuantumCircuit.add_capture` (or the ``captures`` constructor
793
+ argument), but user code will typically use the control-flow builder interface, which handles this
794
+ automatically.
795
+
796
+ Consult :ref:`the control-flow construction documentation <circuit-control-flow-methods>` for more
797
+ information on how to build circuits with control flow.
798
+
799
+ Investigating commutation relations
800
+ -----------------------------------
801
+
802
+ If two operations in a circuit commute, we can swap the order in which they are applied.
803
+ This can allow for optimizations and simplifications, for example, if it allows to merge
804
+ or cancel gates:
805
+
806
+ .. code-block:: text
807
+
808
+ ┌─────────┐ ┌─────────┐ ┌─────────┐
809
+ q_0: ┤ Rz(0.5) ├──■──┤ Rz(1.2) ├──■── q_0: ┤ Rz(1.7) ├
810
+ └─────────┘┌─┴─┐└──┬───┬──┘┌─┴─┐ = └──┬───┬──┘
811
+ q_1: ───────────┤ X ├───┤ X ├───┤ X ├ q_1: ───┤ X ├───
812
+ └───┘ └───┘ └───┘ └───┘
813
+
814
+ Performing these optimizations are part of the transpiler, but the tools to investigate commutations
815
+ are available in the :class:`CommutationChecker`.
816
+
817
+ .. autosummary::
818
+ :toctree: ../stubs/
819
+
820
+ CommutationChecker
821
+
822
+
823
+ .. _circuit-custom-gates:
824
+
825
+ Creating custom instructions
826
+ ============================
827
+
828
+ If you wish to create simple one-off instructions or gates that will be added to a circuit, and the
829
+ blocks are just being used for visualization or grouping purposes, the easiest way to create a
830
+ custom instruction or gate is simply to build its definition as a :class:`QuantumCircuit`, and then
831
+ use its :meth:`~QuantumCircuit.to_instruction` or :meth:`~QuantumCircuit.to_gate` method as
832
+ appropriate. The results can be given directly to :meth:`QuantumCircuit.append` on the larger
833
+ circuit. These methods will create base :class:`Instruction` or :class:`Gate` instances whose
834
+ :attr:`~Instruction.definition` attribute is the circuit as supplied, meaning it will automatically
835
+ be accessible to the transpiler, and to other Qiskit functions that attempt to decompose circuits.
836
+
837
+ Note that standalone instructions and gates should act only on qubits and clbits; instructions that
838
+ need to use complex control-flow will need to be inlined onto the :class:`QuantumCircuit` using
839
+ :meth:`~QuantumCircuit.compose`.
840
+
841
+
842
+ Creating instruction subclasses
843
+ -------------------------------
844
+
845
+ The base classes :class:`Instruction`, :class:`Gate` and :class:`ControlledGate` are all designed to
846
+ be safe to subclass, and have hook points for subclasses to implement. If your custom gate is
847
+ parameterless and stateless, you may also want to derive from the corresponding singleton class in
848
+ :mod:`qiskit.circuit.singleton`, such as :class:`SingletonGate`. You should consult the
849
+ documentation in :mod:`qiskit.circuit.singleton` for additional methods and hook points for the
850
+ singleton machinery.
851
+
852
+ Subclasses should typically define a default constructor that calls the :class`super` constructor
853
+ with the correct arguments for your instruction. It is permissible to have extra state in the
854
+ class, but your subclasses will most reliably integrate with the rest of the Qiskit machinery if you
855
+ depend only on your :attr:`Instruction.params`, and these parameters are purely gate angles.
856
+
857
+ Subclasses of :class:`Instruction` (or one of its subclasses) should implement the private
858
+ :meth:`Instruction._define` method, which lazily populates the hidden ``_definition`` cache that
859
+ backs the public :attr:`~Instruction.definition` method.
860
+
861
+ .. automethod:: Instruction._define
862
+
863
+ In subclasses of :class:`ControlledGate`, the :meth:`~Instruction._define` method should implement
864
+ the decomposition only for the all-ones control state. The :attr:`ControlledGate.definition
865
+ <Instruction.definition>` machinery will modify this to handle the actual control state.
866
+
867
+ If the subclass is using the singleton machinery, beware that :meth:`~Instruction._define` will be
868
+ called eagerly immediately after the class-body statement has been executed, in order to produce the
869
+ definition object for the canonical singleton object. This means that your definition must only use
870
+ gates that are already defined; if you are writing a library with many singleton gates, you will
871
+ have to order your files and imports to ensure that this is possible.
872
+
873
+ Subclasses of :class:`Gate` will also likely wish to override `the Numpy array-protocol instance
874
+ method <https://numpy.org/devdocs/user/basics.interoperability.html#the-array-method>`__,
875
+ ``__array__``. This is used by :meth:`Gate.to_matrix`, and has the signature:
876
+
877
+ .. currentmodule:: None
878
+ .. py:method:: object.__array__(dtype=None, copy=None)
879
+
880
+ Return a Numpy array representing the gate. This can use the gate's
881
+ :attr:`~qiskit.circuit.Instruction.params` field, and may assume that these are numeric
882
+ values (assuming the subclass expects that) and not
883
+ :ref:`compile-time parameters <circuit-compile-time-parameters>`.
884
+
885
+ For greatest efficiency, the returned array should default to a dtype of :class:`complex`.
886
+ .. currentmodule:: qiskit.circuit
887
+
888
+ If your custom subclass has natural representations of its controlled or inverse forms, you may also
889
+ wish to override the :meth:`~Instruction.inverse` and :meth:`~Gate.control` methods.
890
+
891
+
892
+ As an example of defining a custom :math:`R_{xz}` gate; that is, a single-angle rotation about the
893
+ :math:`XZ` axis. This is essentially :class:`RZXGate`, if the qubits were the other way around, so
894
+ we will write our definition in terms of that. We are parametric, so cannot be a singleton, but we
895
+ are unitary, so should be a :class:`Gate`::
896
+
897
+ import math
898
+ import numpy as np
899
+ from qiskit.circuit import Gate, QuantumCircuit
900
+
901
+ class RXZGate(Gate):
902
+ def __init__(self, theta):
903
+ # Initialize with our name, number of qubits and parameters.
904
+ super().__init__("rxz", 2, [theta])
905
+
906
+ def _define(self):
907
+ # Our base definition is an RZXGate, applied "backwards".
908
+ defn = QuantumCircuit(2)
909
+ defn.rzx(1, 0)
910
+ self._definition = defn
911
+
912
+ def inverse(self, annotated = False):
913
+ # We have an efficient representation of our inverse,
914
+ # so we'll override this method.
915
+ return RXZGate(-self.params[0])
916
+
917
+ def power(self, exponent: float):
918
+ # Also we have an efficient representation of power.
919
+ return RXZGate(exponent * self.params[0])
920
+
921
+ def __array__(self, dtype=None, copy=None):
922
+ if copy is False:
923
+ raise ValueError("unable to avoid copy while creating an array as requested")
924
+ cos = math.cos(0.5 * self.params[0])
925
+ isin = 1j * math.sin(0.5 * self.params[0])
926
+ return np.array([
927
+ [cos, -isin, 0, 0],
928
+ [-isin, cos, 0, 0],
929
+ [0, 0, cos, isin],
930
+ [0, 0, isin, cos],
931
+ ], dtype=dtype)
932
+
933
+
934
+ In this example, we defined a base definition in terms of :class:`RZXGate`, but to enable faster
935
+ decompositions to a range of bases, we might want to add some more equivalences to
936
+ :data:`SessionEquivalenceLibrary`. Note that the :class:`.BasisTranslator` translation search will
937
+ search through all possible equivalences at all possible depths, so providing an equivalence in
938
+ terms of (say) :class:`.XGate` will automatically make decompositions in terms of :class:`.RXGate`
939
+ available as well.
940
+
941
+ Let us add an equivalence in terms of :math:`H`, :math:`CX` and :math:`R_z` for an arbitrary symbolic
942
+ parameter::
943
+
944
+ from qiskit.circuit import SessionEquivalenceLibrary, Parameter
945
+
946
+ theta = Parameter("theta")
947
+
948
+ equiv = QuantumCircuit(2)
949
+ equiv.h(0)
950
+ equiv.cx(1, 0)
951
+ equiv.rz(theta, 0)
952
+ equiv.cx(1, 0)
953
+ equiv.h(0)
954
+
955
+ SessionEquivalenceLibrary.add_equivalence(RZXGate(theta), equiv)
956
+
957
+ After this, for the duration of the Python interpreter session, translators like
958
+ :class:`.BasisTranslator` will find our new definition in their search.
959
+
960
+
961
+ .. _circuit-working-with:
962
+
963
+ Working with circuit-level objects
964
+ ==================================
965
+
966
+ .. _circuit-abstract-to-physical:
967
+
968
+ Converting abstract circuits to physical circuits
969
+ -------------------------------------------------
970
+
971
+ ..
972
+ Note that this is just a "jumping-off" section - this should just provide an overview of links
973
+ to where the real information is.
974
+
975
+ An abstract :class:`QuantumCircuit` cannot reliably be run on hardware. You might be able to use
976
+ some of the high-level simulators linked to in :ref:`circuit-simulation` to produce quick results
977
+ for small scale circuits, but to run utility-scale circuits, you will need to use real hardware,
978
+ which involves compiling to a physical circuit.
979
+
980
+ The high-level function to do this is :func:`.transpile`; it takes in an abstract circuit and a
981
+ hardware ``backend`` or ``target``, and returns a physical circuit. To get more access and control
982
+ over the stages of the passes that will be run, use :func:`.generate_preset_pass_manager` to build a
983
+ :class:`~.transpiler.StagedPassManager` first, which you can then modify.
984
+
985
+ The full transpilation and compilation machinery is described in detail in the
986
+ :mod:`qiskit.transpiler` module documentation, and detail on all the passes built into Qiskit is
987
+ available in :mod:`qiskit.transpiler.passes`.
988
+
989
+
990
+ .. _circuit-simulation:
991
+
992
+ Simulating circuits
993
+ -------------------
994
+
995
+ ..
996
+ Note that this is just a "jumping-off" section - this should just provide an overview of links
997
+ to where the real information is.
998
+
999
+
1000
+ While not part of the :mod:`qiskit.circuit` interface, one of the most common needs is to get quick
1001
+ simulation results for :class:`QuantumCircuit` objects. This section provides a quick jumping-off
1002
+ point to other places in the documentation to find the relevant information.
1003
+
1004
+ For unitary circuits, you can simulate the effects on the :math:`\lvert0\dotsm0\rangle` state by
1005
+ passing the :class:`QuantumCircuit` directly to the :class:`~.quantum_info.Statevector` default
1006
+ constructor. You can similar get a unitary matrix representing the circuit as an operator by
1007
+ passing it to the :class:`~.quantum_info.Operator` default constructor. If you have a physical
1008
+ circuit, you may want to instead pass it to :meth:`.Operator.from_circuit` method to apply
1009
+ transformations from the :attr:`QuantumCircuit.layout` to map it back to the "abstract" qubit space.
1010
+
1011
+ For a more backend-like simulation experience, there are simulator-backed implementations of all the
1012
+ Qiskit hardware interfaces. In particular, you might be interested in:
1013
+
1014
+ * :class:`.BasicProvider` and the raw backends it can return to you.
1015
+ * :class:`.StatevectorSimulator` for a backend-like wrapper around :class:`.Statevector`
1016
+ * The :mod:`qiskit_aer` for full, high-performance simulation capabilities.
1017
+ * :class:`.StatevectorSampler` and :class:`.StatevectorEstimator` for simulator-backed reference
1018
+ implementations of the :ref:`Qiskit Primitives <qiskit-primitives>`.
1019
+
1020
+
1021
+ Defining equivalence relationships
1022
+ ----------------------------------
1023
+
1024
+ A common task in mapping abstract circuits to physical hardware and optimizing the result is to find
1025
+ equivalence relations that map a gate to a different basis set. Qiskit stores this information in a
1026
+ database class called :class:`EquivalenceLibrary`.
1027
+
1028
+ .. autosummary::
1029
+ :toctree: ../stubs/
1030
+
1031
+ EquivalenceLibrary
1032
+
1033
+ Qiskit ships with a large set of predefined equivalence relationships for all of its standard gates.
1034
+ This base library is called :data:`StandardEquivalenceLibrary`, and should be treated as immutable.
1035
+
1036
+ .. py:data:: StandardEquivalenceLibrary
1037
+
1038
+ A :class:`EquivalenceLibrary` that stores of all Qiskit's built-in standard gate relationships.
1039
+ You should not mutate this, but instead either create your own :class:`EquivalenceLibrary` using
1040
+ this one as its ``base``, or modify the global-state :data:`SessionEquivalenceLibrary`.
1041
+
1042
+ Qiskit also defines a shared global-state object, :data:`SessionEquivalenceLibrary`, which is the
1043
+ default equivalences used by various places in Qiskit, most notably the :class:`.BasisTranslator`
1044
+ transpiler pass. You should feel free to add your own equivalences to this using its
1045
+ :meth:`~EquivalenceLibrary.add_equivalence` method, and they will be automatically picked up by
1046
+ default instances of the :class:`.BasisTranslator`.
1047
+
1048
+ .. py:data:: SessionEquivalenceLibrary
1049
+
1050
+ The default instance of :class:`EquivalenceLibrary`, which will be used by most Qiskit objects
1051
+ if no library is manually specified. You can feel free to add equivalences to this using
1052
+ :meth:`~EquivalenceLibrary.add_equivalence`. It inherits all the built-in rules of
1053
+ :data:`StandardEquivalenceLibrary`.
1054
+
1055
+
1056
+
1057
+ Generating random circuits
1058
+ --------------------------
1059
+
1060
+ ..
1061
+ If we expand these capabilities in the future, it's probably best to move it to its own
1062
+ module-level documentation page than to expand this "inline" module documentation.
1063
+
1064
+ .. currentmodule:: qiskit.circuit.random
1065
+ .. autofunction:: random_circuit
1066
+ .. autofunction:: random_clifford_circuit
1067
+ .. currentmodule:: qiskit.circuit
1068
+
1069
+ Apply Pauli twirling to a circuit
1070
+ ---------------------------------
1071
+
1072
+ There are two primary types of noise when executing quantum circuits. The first is stochastic,
1073
+ or incoherent, noise that is mainly due to the unwanted interaction between the quantum processor
1074
+ and the external environment in which it resides. The second is known as coherent error, and these
1075
+ errors arise due to imperfect control of a quantum system. This can be unwanted terms in a system
1076
+ Hamiltonian, i.e. incorrect unitary evolution, or errors from incorrect temporal control of the
1077
+ quantum system, which includes things like incorrect pulse-shapes for gates.
1078
+
1079
+ Pauli twirling is a quantum error suppression technique that uses randomization to shape coherent
1080
+ error into stochastic errors by combining the results from many random, but logically equivalent
1081
+ circuits, together. Qiskit provides a function to apply Pauli twirling to a given circuit for
1082
+ standard two qubit gates. For more details you can refer to the documentation of the function
1083
+ below:
1084
+
1085
+ .. autofunction:: qiskit.circuit.pauli_twirl_2q_gates
1086
+
1087
+
1088
+ Exceptions
1089
+ ==========
1090
+
1091
+ Almost all circuit functions and methods will raise a :exc:`CircuitError` when encountering an error
1092
+ that is particular to usage of Qiskit (as opposed to regular typing or indexing problems, which will
1093
+ typically raise the corresponding standard Python error).
1094
+
1095
+ .. autoexception:: CircuitError
1096
+
1097
+
1098
+ .. _circuit-conventions:
1099
+
1100
+ Circuit conventions
1101
+ ===================
1102
+
1103
+ When constructing circuits out of abstract objects and more concrete matrices, there are several
1104
+ possible conventions around bit-labelling, bit-ordering, and how the abstract tensor product is
1105
+ realized in concrete matrix algebra.
1106
+
1107
+ Qiskit's conventions are:
1108
+
1109
+ * in bitstring representations, bits are labelled with the right-most bit in the string called
1110
+ :math:`0` and the left-most bit in the string of :math:`n` bits called :math:`n - 1`.
1111
+
1112
+ * when using integers as bit-specifier indices in circuit-construction functions, the integer is
1113
+ treated as an index into :attr:`QuantumCircuit.qubits` (or :attr:`~QuantumCircuit.clbits`).
1114
+
1115
+ * when drawing circuits, we put the lowest-index bits on top.
1116
+
1117
+ * in statevector representations, we realize the abstract tensor product as the Kronecker product,
1118
+ and order the arguments to this such that the amplitude of computational-basis state
1119
+ :math:`\lvert x\rangle`, where :math:`x` is the bitstring interpreted as an integer, is at
1120
+ location ``statevector[x]``.
1121
+
1122
+ * when controlling a gate, the control qubit(s) is placed first in the argument list, *e.g.* in the
1123
+ call ``qc.cx(0, 1)``, qubit 0 will be the control and qubit 1 will be the target. Similarly, in
1124
+ the manual call ``qc.append(CXGate(), [0, 1])``, qubit 0 will be the control and qubit 1 will be
1125
+ the target.
1126
+
1127
+ Let us illustrate these conventions with some examples.
1128
+
1129
+ Bit labelling
1130
+ -------------
1131
+
1132
+ Take the circuit:
1133
+
1134
+ .. plot::
1135
+ :include-source:
1136
+ :nofigs:
1137
+ :context:
1138
+ :show-source-link: False
1139
+
1140
+ from qiskit import QuantumCircuit
1141
+
1142
+ qc = QuantumCircuit(5, 5)
1143
+ qc.x(0)
1144
+ qc.x(1)
1145
+ qc.x(4)
1146
+ qc.measure(range(5), range(5))
1147
+
1148
+ This flips the states of qubits 0, 1 and 4 from :math:`\lvert0\rangle` to :math:`\lvert1\rangle`,
1149
+ then measures all qubits :math:`n` into the corresponding clbit :math:`n` using the computational
1150
+ (:math:`Z`) basis. If simulated noiselessly, the bitstring output from this circuit will be
1151
+ :math:`10011` every time; qubits 0, 1, and 4 are flipped, and the "one" values in the bitstring are
1152
+ in the zeroth, first and fourth digits *from the right*.
1153
+
1154
+ In Qiskit, we would write the qubit state immediately before the measurement in ket-notation
1155
+ shorthand as :math:`\lvert10011\rangle`. Note that the ket label matches the classical bitstring,
1156
+ and has the numeric binary value of 19.
1157
+
1158
+ If we draw this circuit, we will see that Qiskit places the zeroth qubit on the top of the circuit
1159
+ drawing:
1160
+
1161
+ .. plot::
1162
+ :alt: Circuit diagram output by the previous code.
1163
+ :include-source:
1164
+ :context:
1165
+ :show-source-link: False
1166
+
1167
+ qc.draw("mpl")
1168
+
1169
+
1170
+ Matrix representations
1171
+ ----------------------
1172
+
1173
+ Statevectors are defined in the convention that for a two-level system, the relationship between
1174
+ abstract representation and matrix representation is such that
1175
+
1176
+ .. math::
1177
+
1178
+ \alpha\lvert0\rangle + \beta\lvert1\rangle
1179
+ \leftrightarrow \begin{pmatrix} \alpha \\ \beta \end{pmatrix}
1180
+
1181
+ where :math:`\alpha` and :math:`\beta` are complex numbers. We store the statevector as a 1D Numpy
1182
+ :class:`~numpy.ndarray` with data ``sv = [alpha, beta]``, *i.e.* ``sv[0] == alpha`` and ``sv[1] ==
1183
+ beta``; note that the indices into the statevector match the ket labels.
1184
+
1185
+ We construct `the tensor product of two qubit states
1186
+ <https://en.wikipedia.org/wiki/Tensor_product>`_ in matrix algebra using `the Kronecker product
1187
+ <https://en.wikipedia.org/wiki/Kronecker_product>`_, with qubit 0 on the right and qubit 1 on the
1188
+ left, such that the :math:`Z` basis state :math:`\lvert x\rangle` (where :math:`x` is the integer
1189
+ interpretation of the bitstring) has its non-zero term in the statevector ``sv`` at ``sv[x]``::
1190
+
1191
+ import numpy
1192
+ from qiskit import QuantumCircuit
1193
+ from qiskit.quantum_info import Statevector
1194
+
1195
+ state_0 = [1, 0] # defined representation of |0>
1196
+ state_1 = [0, 1] # defined representation of |1>
1197
+
1198
+ # Circuit that creates basis state |10011>, where
1199
+ # binary 10011 has the decimal value 19.
1200
+ qc = QuantumCircuit(5)
1201
+ qc.x(0)
1202
+ qc.x(1)
1203
+ qc.x(4)
1204
+ qiskit_sv = Statevector(qc)
1205
+
1206
+ # List index 'n' corresponds to qubit 'n'.
1207
+ individual_states = [
1208
+ state_1,
1209
+ state_1,
1210
+ state_0,
1211
+ state_0,
1212
+ state_1,
1213
+ ]
1214
+ # Start from a scalar.
1215
+ manual_sv = [1]
1216
+ for qubit_state in individual_states:
1217
+ # Each new qubit goes "on the left".
1218
+ manual_sv = numpy.kron(qubit_state, manual_sv)
1219
+
1220
+ # Now `qiskit_sv` and `manual_sv` are the same, and:
1221
+ assert manual_sv[19] == 1
1222
+ assert qiskit_sv[19] == 1
1223
+
1224
+ This feeds through to the matrix representation of operators, and joins with the conventions on bit
1225
+ orders for controlled operators. For example, the matrix form of :class:`.CXGate` is::
1226
+
1227
+ import numpy
1228
+ from qiskit.circuit.library import CXGate
1229
+
1230
+ numpy.array(CXGate())
1231
+
1232
+ .. math::
1233
+
1234
+ \operatorname{array}(CX) =
1235
+ \begin{pmatrix}
1236
+ 1 & 0 & 0 & 0 \\
1237
+ 0 & 0 & 0 & 1 \\
1238
+ 0 & 0 & 1 & 0 \\
1239
+ 0 & 1 & 0 & 0
1240
+ \end{pmatrix}
1241
+
1242
+ This might be different to other matrix representations you have seen for :math:`CX`, but recall
1243
+ that the choice of matrix representation is conventional, and this form matches Qiskit's conventions
1244
+ of *control qubits come first* and *the tensor product is represented such that there is a
1245
+ correspondence between the index of the "one amplitude" and the bitstring value of a state*.
1246
+
1247
+ In the case of multiple controls for a gate, such as for :class:`.CCXGate`, the ``ctrl_state``
1248
+ argument is interpreted as the bitstring value of the control qubits, using the same zero-based
1249
+ labelling conventions. For example, given that the default ``ctrl_state`` is the all-ones
1250
+ bitstring, we can see that the matrix form of :class:`.CCXGate` with ``ctrl_state = 1`` is the same
1251
+ as if we took the all-ones control-state :class:`.CCXGate`, but flipped the value of the higher
1252
+ indexed control qubit on entry and exist to the gate::
1253
+
1254
+ from qiskit import QuantumCircuit
1255
+ from qiskit.quantum_info import Operator
1256
+
1257
+ # Build the natural representation of `CCX` with the
1258
+ # control qubits being `[0, 1]`, relative to the
1259
+ # bitstring state "01", such that qubit 0 must be in |1>
1260
+ # and qubit 1 must be in |0>. The target qubit is 2.
1261
+ ccx_natural = QuantumCircuit(3)
1262
+ ccx_natural.ccx(0, 1, 2, ctrl_state=1)
1263
+
1264
+ # Build the same circuit in terms of the all-ones CCX.
1265
+ # Note that we flip _qubit 1_, because that's the one
1266
+ # that differs from the all-ones state.
1267
+ ccx_relative = QuantumCircuit(3)
1268
+ ccx_relative.x(1)
1269
+ ccx_relative.ccx(0, 1, 2)
1270
+ ccx_relative.x(1)
1271
+
1272
+ assert Operator(ccx_relative) == Operator(ccx_natural)
1273
+
1274
+ In both these cases, the matrix form of :class:`.CCXGate` in ``ctrl_state = 1`` is:
1275
+
1276
+ .. math::
1277
+
1278
+ \operatorname{array}\bigl(CCX(\text{ctrl\_state}=1)\bigr) =
1279
+ \begin{pmatrix}
1280
+ 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
1281
+ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\
1282
+ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\
1283
+ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\
1284
+ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\
1285
+ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\
1286
+ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\
1287
+ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
1288
+ \end{pmatrix}
1289
+ """
1290
+
1291
+ from qiskit._accelerate.circuit import ( # pylint: disable=unused-import
1292
+ Bit,
1293
+ Qubit,
1294
+ AncillaQubit,
1295
+ Clbit,
1296
+ QuantumRegister,
1297
+ AncillaRegister,
1298
+ ClassicalRegister,
1299
+ Register,
1300
+ Duration,
1301
+ )
1302
+
1303
+ from .exceptions import CircuitError
1304
+ from . import _utils
1305
+ from .quantumcircuit import QuantumCircuit
1306
+ from .gate import Gate
1307
+
1308
+ # pylint: disable=cyclic-import
1309
+ from .controlledgate import ControlledGate
1310
+ from . import singleton
1311
+ from .instruction import Instruction
1312
+ from .instructionset import InstructionSet
1313
+ from .operation import Operation
1314
+ from .barrier import Barrier
1315
+ from .delay import Delay
1316
+ from .measure import Measure
1317
+ from .reset import Reset
1318
+ from .store import Store
1319
+ from .parameter import Parameter
1320
+ from .parametervector import ParameterVector, ParameterVectorElement
1321
+ from .parameterexpression import ParameterExpression
1322
+ from .quantumcircuitdata import CircuitInstruction
1323
+ from .equivalence import EquivalenceLibrary
1324
+ from . import library
1325
+ from .equivalence_library import StandardEquivalenceLibrary, SessionEquivalenceLibrary
1326
+ from .commutation_checker import CommutationChecker
1327
+
1328
+ from .controlflow import (
1329
+ ControlFlowOp,
1330
+ BoxOp,
1331
+ WhileLoopOp,
1332
+ ForLoopOp,
1333
+ IfElseOp,
1334
+ SwitchCaseOp,
1335
+ CASE_DEFAULT,
1336
+ BreakLoopOp,
1337
+ ContinueLoopOp,
1338
+ CONTROL_FLOW_OP_NAMES,
1339
+ get_control_flow_name_mapping,
1340
+ )
1341
+
1342
+ from .annotated_operation import AnnotatedOperation, InverseModifier, ControlModifier, PowerModifier
1343
+ from .twirling import pauli_twirl_2q_gates