mindquantum.core.gates.Measure
- class mindquantum.core.gates.Measure(name='', reset_to=None)[源代码]
测量量子比特的测量门。
- 参数:
name (str) - 此测量门的键。在量子线路中,不同测量门的键应该是唯一的。默认值:
''
。reset_to (Union[int, None]) - 将量子比特重置为0态或者1态。如果是
None
,则不重置量子比特。默认值:None
。
样例:
>>> import numpy as np >>> from mindquantum.algorithm.library import qft >>> from mindquantum.core.circuit import Circuit >>> from mindquantum.core.gates import Measure >>> from mindquantum.simulator import Simulator >>> circ = qft(range(2)) >>> circ += Measure('q0').on(0) >>> circ += Measure().on(1) >>> circ ┏━━━┓ ┏━━━━━━━━━┓ ┍━━━━━━┑ q0: ──┨ H ┠─┨ PS(π/2) ┠───────╳─┤ M q0 ├─── ┗━━━┛ ┗━━━━┳━━━━┛ ┃ ┕━━━━━━┙ ┃ ┏━━━┓ ┃ ┍━━━━━━┑ q1: ─────────────■──────┨ H ┠─╳─┤ M q1 ├─── ┗━━━┛ ┕━━━━━━┙ >>> sim = Simulator('mqvector', circ.n_qubits) >>> sim.apply_circuit(Circuit().h(0).x(1, 0)) >>> sim mqvector simulator with 2 qubits (little endian). Current quantum state: √2/2¦00⟩ √2/2¦11⟩ >>> res = sim.sampling(circ, shots=2000, seed=42) >>> res shots: 2000 Keys: q1 q0│0.00 0.124 0.248 0.372 0.496 0.621 ───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴ 00│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ │ 10│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ │ 11│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒ │ {'00': 993, '10': 506, '11': 501} >>> sim mqvector simulator with 2 qubits (little endian). Current quantum state: √2/2¦00⟩ √2/2¦11⟩ >>> sim.apply_circuit(circ[:-2]) >>> sim mqvector simulator with 2 qubits (little endian). Current quantum state: √2/2¦00⟩ (√2/4-√2/4j)¦10⟩ (√2/4+√2/4j)¦11⟩ >>> np.abs(sim.get_qs())**2 array([0.5 , 0. , 0.25, 0.25])