mindquantum.utils

Utils

mindquantum.utils.bprint(strings: list, align=":", title="", v_around="=", h_around="|", fill_char=" ")[source]

Print the information in block shape.

Parameters
  • strings (list[str]) – Information you want to output.

  • align (str) – The align char alone vertal direction.

  • title (str) – The title of this information block.

  • v_around (str) – Vertical boundary char.

  • h_around (str) – horizontal boundary char.

  • fill_char (str) – Empty space fill with this char.

Returns

list(str), Formatted string.

Examples

>>> title='Info of Bob'
>>> o = bprint(['Name:Bob', 'Age:17', 'Nationality:China'],
>>>     title=title)
>>> for i in o:
>>>     print(i)
====Info of Bob====
|Name       :Bob  |
|Age        :17   |
|Nationality:China|
===================
mindquantum.utils.mod(vec_in, axis=0)[source]

Calculate the mod of input vectors.

Parameters
  • vec_in (Union[list[number], numpy.ndarray]) – The vector you want to calculate mod.

  • axis (int) – Along which axis you want to calculate mod.

Returns

numpy.ndarray, The mod of input vector.

Examples

>>> vec_in = np.array([[1, 2, 3], [4, 5, 6]])
>>> mod(vec_in)
array([[4.12310563, 5.38516481, 6.70820393]])
>>> mod(vec_in, 1)
array([[3.74165739],
       [8.77496439]])
mindquantum.utils.normalize(vec_in, axis=0)[source]

Normalize the input vectors based on specified axis.

Parameters
  • vec_in (Union[list[number], numpy.ndarray]) – Vector you want to normalize.

  • axis (int) – Along which axis you want to normalize your vector.

Returns

numpy.ndarray, Vector after normalization.

Examples

>>> vec_in = np.array([[1, 2, 3], [4, 5, 6]])
>>> normalize(vec_in)
array([[0.24253563, 0.37139068, 0.4472136 ],
       [0.9701425 , 0.92847669, 0.89442719]])
>>> normalize(vec_in, 1)
array([[0.26726124, 0.53452248, 0.80178373],
       [0.45584231, 0.56980288, 0.68376346]])
mindquantum.utils.random_state(shapes, norm_axis=0, comp=True, seed=None)[source]

Generate some random quantum state.

Parameters
  • shapes (tuple) – shapes = (m, n) means m quantum states with each state formed by log2(n) qubits.

  • norm_axis (int) – which axis you want to apply normalization. Default, 0

  • comp (bool) – if True, each amplitude of the quantum state will be a complex number.

  • seed (int) – the random seed.

Returns

numpy.ndarray, A normalized random quantum state.

Examples

>>> random_state((2, 2), seed=42)
array([[0.44644744+0.18597239j, 0.66614846+0.10930256j],
       [0.87252821+0.06923499j, 0.41946926+0.60691409j]])