mindquantum.algorithm.library.amplitude_encoder

View Source On Gitee
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', 2)
>>> encoder, parameterResolver = amplitude_encoder([0.5, -0.5, -0.5j, -0.5j], 2)
>>> sim.apply_circuit(encoder, parameterResolver)
>>> print(sim.get_qs(True))
1/2¦00⟩
-1/2¦01⟩
-1/2j¦10⟩
-1/2j¦11⟩
>>> sim.reset()
>>> encoder, parameterResolver = amplitude_encoder([0, -0.5j, -0.5j, -0.5, 0.5], 3)
>>> sim = Simulator('mqvector', 3)
>>> sim.apply_circuit(encoder, parameterResolver)
>>> print(sim.get_qs(True))
-1/2j¦001⟩
-1/2j¦010⟩
-1/2¦011⟩
1/2¦100⟩