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. Default: “:”.

  • title (str) – The title of this information block. Default: “”.

  • v_around (str) – Vertical boundary char. Default: “=”.

  • h_around (str) – horizontal boundary char. Default: “|”.

  • fill_char (str) – Empty space fill with this char. Default: ” “.

Returns

list, a list of formatted string.

Examples

>>> from mindquantum.utils import bprint
>>> 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.commutator(left_operator, right_operator)[source]

Compute the commutator of two operators.

Parameters
Raises

TypeError – operator_a and operator_b are not of the same type.

Examples

>>> from mindquantum.ops import QubitOperator,FermionOperator
>>> from mindquantum.utils import commutator
>>> qub_op1 = QubitOperator("X1 Y2")
>>> qub_op2 = QubitOperator("X1 Z2")
>>> commutator(qub_op1, qub_op1)
0
>>> commutator(qub_op1, qub_op2)
2j [X2]
mindquantum.utils.count_qubits(operator)[source]

Calculate the number of qubits on which operator acts before removing the unused qubit

Note

In some case, we need to remove the unused index.

Parameters

operator (Union[FermionOperator, QubitOperator, QubitExcitationOperator]) – FermionOperator or QubitOperator or QubitExcitationOperator.

Returns

int, The minimum number of qubits on which operator acts.

Raises

TypeError – Operator of invalid type.

Examples

>>> from mindquantum.ops import QubitOperator,FermionOperator
>>> from mindquantum.utils import count_qubits
>>> qubit_op = QubitOperator("X1 Y2")
>>> count_qubits(qubit_op)
3
>>> fer_op = FermionOperator("1^")
>>> count_qubits(fer_op)
2
mindquantum.utils.down_index(index)[source]

The index order, by default we set the spinless orbits as even-odd-even-odd (0,1,2,3,…). The spin_down orbitals (beta orbital) with index odd.

Parameters

index (int) – spatial orbital index.

Returns

An integer that is the index of the associated spin-down orbital.

Examples

>>> from mindquantum.utils import down_index
>>> down_index(1)
3
mindquantum.utils.get_fermion_operator(operator)[source]

Convert the tensor (PolynomialTensor) to FermionOperator.

Returns

fermion_operator, An instance of the FermionOperator class.

mindquantum.utils.hermitian_conjugated(operator)[source]

Return Hermitian conjugate of FermionOperator or QubitOperator.

Parameters

operator (Union[FermionOperator, QubitOperator, QubitExcitationOperator]) – The input operator.

Returns

operator (Union[FermionOperator, QubitOperator, QubitExcitationOperator]), the hermitian form of the input operator.

Examples

>>> from mindquantum.ops import QubitOperator
>>> from mindquantum.utils import hermitian_conjugated
>>> q = QubitOperator('X0', {'a' : 2j})
>>> hermitian_conjugated(q)
-2.0*I*a [X0]
mindquantum.utils.ket_string(state, tol=1e-07)[source]

Get the ket format of the quantum state.

Parameters
  • state (numpy.ndarray) – The input quantum state.

  • tol (float) – The ignore tolence for small amplitude. Default: 1e-7.

Returns

str, the ket format of the quantum state.

Examples

>>> import numpy as np
>>> from mindquantum.utils import ket_string
>>> state = np.array([1, -1j])/np.sqrt(2)
>>> print('\n'.join(ket_string(state)))
√2/2¦0⟩
-√2/2j¦1⟩
mindquantum.utils.mod(vec_in, axis=0)[source]

Calculate the mod of input vectors.

Parameters
Returns

numpy.ndarray, The mod of input vector.

Examples

>>> from mindquantum.utils import mod
>>> 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.normal_ordered(fermion_operator)[source]

Calculate and return the normal order of the FermionOperator. By convention, normal ordering implies terms are ordered from highest mode index (on left) to lowest (on right). Also, creation operators come first then follows the annihilation operator. e.g 3 4^ \(\rightarrow\) - 4^ 3.

Parameters

fermion_operator (FermionOperator) – Only Fermion type Operator has such forms.

Returns

FermionOperator, the normal_ordered FermionOperator.

Examples

>>> from mindquantum.ops import FermionOperator
>>> from mindquantum.utils import normal_ordered
>>> op = FermionOperator("3 4^", 'a')
>>> normal_ordered(op)
-a [4^ 3]
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. Default: 0.

Returns

numpy.ndarray, Vector after normalization.

Examples

>>> from mindquantum.utils import normalize
>>> 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.number_operator(n_modes=None, mode=None, coefficient=1.0)[source]

Return a fermionic number operator for the reverse_jordan_wigner transform.

Parameters
  • n_modes (int) – The number of modes in the system. Default: None.

  • mode (int, optional) – The mode on which to return the number operator. If None, return total number operator on all sites. Default: None.

  • coefficient (float) – The coefficient of the term. Default: 1.

Returns

FermionOperator, a fermionic number operator for the reverse_jordan_wigner transform.

Examples

>>> from mindquantum.ops import FermionOperator
>>> from mindquantum.utils import number_operator
>>> nmode = 3
>>> number_operator(nmode)
1.0 [0^ 0] +
1.0 [1^ 1] +
1.0 [2^ 2]
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 \(\log_2(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. Default: True.

  • seed (int) – the random seed. Default: None.

Returns

numpy.ndarray, A normalized random quantum state.

Examples

>>> from mindquantum.utils import random_state
>>> random_state((2, 2), seed=42)
array([[0.44644744+0.18597239j, 0.66614846+0.10930256j],
       [0.87252821+0.06923499j, 0.41946926+0.60691409j]])
mindquantum.utils.sz_operator(n_spatial_orbitals)[source]

Return the sz operator.

Parameters

n_spatial_orbitals (int) – number of spatial orbitals (n_qubits // 2).

Returns

FermionOperator, corresponding to the sz operator over n_spatial_orbitals.

Note

The default index order spin_up(alpha) corresponds to even index, while the spin_down(beta) corresponds to odd index.rpartition()

Examples

>>> from mindquantum.utils import sz_operator
>>> sz_operator(3)
0.5 [0^ 0] +
-0.5 [1^ 1] +
0.5 [2^ 2] +
-0.5 [3^ 3] +
0.5 [4^ 4] +
-0.5 [5^ 5]
mindquantum.utils.up_index(index)[source]

The index order, by default we set the spinless orbits as even-odd-even-odd (0,1,2,3,…). The spin_up orbitals (alpha orbitals) with index even.

Parameters

index (int) – spatial orbital index.

Returns

An integer that is the index of the associated spin-up orbital.

Examples

>>> from mindquantum.utils import up_index
>>> up_index(1)
2