mindquantum.core.gates.MeasureResult

View Source On Gitee
class mindquantum.core.gates.MeasureResult[source]

Measurement result container.

Examples

>>> from mindquantum.algorithm.library import qft
>>> from mindquantum.simulator import Simulator
>>> sim = Simulator('mqvector', 2)
>>> res = sim.sampling(qft(range(2)).measure_all(), shots=1000, seed=42)
>>> res
shots: 1000
Keys: q1 q0│0.00   0.065        0.13       0.194       0.259       0.324
───────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
         00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         01│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         10│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

         11│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

{'00': 230, '01': 254, '10': 257, '11': 259}
>>> res.data
{'00': 230, '01': 254, '10': 257, '11': 259}
add_measure(measure)[source]

Add a measurement gate into this measurement result container.

Measure key should be unique in this measurement result container.

Parameters

measure (Union[Iterable, Measure]) – One or more measure gates.

collect_data(samples)[source]

Collect the measured bit string.

Parameters

samples (numpy.ndarray) – A two dimensional (N x M) numpy array that stores the sampling bit string in 0 or 1, where N represents the number of shot times, and M represents the number of keys in this measurement container

property data

Get the sampling data.

Returns

dict, The sampling data.

property keys_map

Reverse mapping for the keys.

select_keys(*keys)[source]

Select certain measurement keys from this measurement container.

Parameters

keys (tuple[str]) – The key you want to select.

Examples

>>> from mindquantum.algorithm.library import qft
>>> from mindquantum.core.gates import H
>>> from mindquantum.simulator import Simulator
>>> circ = qft(range(2)).measure('q0_0', 0).measure('q1_0', 1)
>>> circ.h(0).measure('q0_1', 0)
>>> circ
      ┏━━━┓ ┏━━━━━━━━━┓         ┍━━━━━━━━┑ ┏━━━┓ ┍━━━━━━━━┑
q0: ──┨ H ┠─┨ PS(π/2) ┠───────╳─┤ M q0_0 ├─┨ H ┠─┤ M q0_1 ├───
      ┗━━━┛ ┗━━━━┳━━━━┛       ┃ ┕━━━━━━━━┙ ┗━━━┛ ┕━━━━━━━━┙
                 ┃      ┏━━━┓ ┃ ┍━━━━━━━━┑
q1: ─────────────■──────┨ H ┠─╳─┤ M q1_0 ├────────────────────
                        ┗━━━┛   ┕━━━━━━━━┙
>>> sim = Simulator('mqvector', circ.n_qubits)
>>> res = sim.sampling(circ, shots=500, seed=42)
>>> new_res = res.select_keys('q0_1', 'q1_0')
>>> new_res
shots: 500
Keys: q1_0 q0_1│0.00   0.068       0.136       0.204       0.272        0.34
───────────────┼───────────┴───────────┴───────────┴───────────┴───────────┴
             00│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

             01│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

             10│▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓

             11│▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

{'00': 127, '01': 107, '10': 136, '11': 130}
svg(style=None)[source]

Display current measurement result into SVG picture in jupyter notebook.

Parameters

style (dict, str) – the style to set svg style. Currently, we support 'official'. Default: None.