mindquantum.core.gates.Measure

View Source On Gitee
class mindquantum.core.gates.Measure(name='', reset_to=None)[source]

Measurement gate that measure quantum qubits.

Parameters
  • name (str) – The key of this measurement gate. In a quantum circuit, the key of different measurement gate should be unique. Default: ''.

  • reset_to (Union[int, None]) – Reset the qubit to 0 state or 1 state. If None, do not reset. Default: None.

Examples

>>> 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])
get_cpp_obj()[source]

Get the underlying C++ object.

hermitian()[source]

Hermitian gate of measure return itself.

on(obj_qubits, ctrl_qubits=None)[source]

Define which qubit the gate act on and the control qubit.

Parameters
  • obj_qubits (Union[int, list[int]]) – measure on which qubit.

  • ctrl_qubits (Union[int, list[int]]) – for measurement, we can not set control qubits. Default: None.

Returns

Measure, a measurement gate with will defined obj_qubits .