mindquantum.algorithm.library

Circuit library.

mindquantum.algorithm.library.amplitude_encoder(x, n_qubits)[source]

Quantum circuit for amplitude encoding.

Note

The length of classic data ought to be the power of 2, otherwise will be filled up with 0. The vector should be normalized.

Parameters
  • x (list[float] or numpy.array(list[float])) – the vector of data you want to encode, which should be normalized.

  • n_qubits (int) – the number of qubits of the encoder circuit.

Returns

Circuit, the parameterized quantum circuit that do amplitude encoder. ParameterResolver, the parameter for parameterized quantum circuit to do amplitude encoder.

Examples

>>> from mindquantum.algorithm.library import amplitude_encoder
>>> from mindquantum.simulator import Simulator
>>> sim = Simulator('mqvector', 8)
>>> encoder, parameterResolver = amplitude_encoder([0.5, -0.5, 0.5, 0.5], 8)
>>> sim.apply_circuit(encoder, parameterResolver)
>>> print(sim.get_qs(True))
1/2¦00000000⟩
-1/2¦00000001⟩
1/2¦00000010⟩
1/2¦00000011⟩
>>> sim.reset()
>>> encoder, parameterResolver = amplitude_encoder([0, 0, 0.5, 0.5, -0.5, 0.5], 8)
>>> sim.apply_circuit(encoder, parameterResolver)
>>> print(sim.get_qs(True))
1/2¦00000010⟩
1/2¦00000011⟩
-1/2¦00000100⟩
1/2¦00000101⟩
mindquantum.algorithm.library.bitphaseflip_operator(phase_inversion_index, n_qubits)[source]

Generate a circuit that can flip the sign of any calculation bases.

Parameters
  • phase_inversion_index (list[int]) – Index of calculation bases want to flip phase.

  • n_qubits (int) – Total number of qubits.

Examples

>>> from mindquantum.core.circuit import Circuit, UN
>>> from mindquantum.core.gates import H, Z
>>> from mindquantum.algorithm.library import bitphaseflip_operator
>>> circuit = Circuit()
>>> circuit += UN(H, 3)
>>> circuit += bitphaseflip_operator([1, 3], 3)
>>> print(circuit.get_qs(ket=True))
√2/4¦000⟩
-√2/4¦001⟩
√2/4¦010⟩
-√2/4¦011⟩
√2/4¦100⟩
√2/4¦101⟩
√2/4¦110⟩
√2/4¦111⟩
Returns

Circuit, the bit phase flip circuit.

mindquantum.algorithm.library.general_ghz_state(qubits)[source]

Circuit that prepare a general GHZ State based on zero state.

The GHZ State is defined as the equality superposition of three zeros state and three ones state:

Here in this API, we can create a general GHZ state on arbitrary sub qubits of any total qubits.

Parameters

qubits (list[int]) – Qubits you want to apply general GHZ state.

Examples

>>> from mindquantum.algorithm.library import general_ghz_state
>>> print(general_ghz_state(range(3)).get_qs(ket=True))
√2/2¦000⟩
√2/2¦111⟩
>>> print(general_ghz_state([1, 2]).get_qs(ket=True))
√2/2¦000⟩
√2/2¦110⟩
Returns

Circuit, circuit that can prepare ghz state.

mindquantum.algorithm.library.general_w_state(qubits)[source]

General W State.

The W State is defined as the equality superposition of bases that only one qubit is in \(\left|1\right>\) while others qubits are in \(\left|0\right>\). For example, a three qubits W state is defined as:

\[\left|\rm W\right> = (\left|001\right> + \left|010\right> + \left|100\right>)/\sqrt(3)\]

Here in this API, we can define a W state on any sub hilbert space of any total number qubits.

Parameters

qubits (list[int]) – Qubits you want to apply general W state.

Examples

>>> from mindquantum.algorithm.library import general_w_state
>>> print(general_w_state(range(3)).get_qs(ket=True))
0.5773502691896257¦001⟩
0.5773502691896258¦010⟩
0.5773502691896257¦100⟩
Returns

Circuit, circuit that can prepare w state.

mindquantum.algorithm.library.qft(qubits)[source]

Quantum fourier transform (QFT).

The function of the quantum Fourier transform is similar to that of the classical Fourier transform.

Note

Please refer to Nielsen, M., & Chuang, I. (2010) for more information.

Parameters

qubits (list[int]) – Qubits you want to apply quantum fourier transform.

Examples

>>> from mindquantum.algorithm.library import qft
>>> print(qft([0, 1]).get_qs(ket=True))
1/2¦00⟩
1/2¦01⟩
1/2¦10⟩
1/2¦11⟩
Returns

Circuit, circuit that can do fourier transform.