I want to encode two vectors into qubits and compute a distance between them that corresponds/is proportional to the euclidian distance of the real vectors.

The encoding I use is

  • A) qiskit initialize method
def get_encoding_circuit(*vv): 
    sz = int(np.log2(len(vv[0])))
    qc = QuantumCircuit(len(vv)*sz)

    for i, v in enumerate(vv):
        qc.initialize(v, range(i*sz, (i+1)*sz))
    return qc


  • B) amplitude encoding as shown here

The distance metric I use is given by the overlap from

  • I) qiskit's state_fidelity method and
  • II) a Swap Test circuit.

For example in $\mathbb{R^2}$, $u$ and $v$ given as

$$ u = \begin{bmatrix} 1 \\ 1 \end{bmatrix} \quad v = \begin{bmatrix} -1 \\ -1 \end{bmatrix} $$

are encoded as

$$ |u\rangle = \begin{bmatrix} 0.70711 \\ 0.70711 \end{bmatrix} \quad |v\rangle = \begin{bmatrix} - 0.70711 \\ - 0.70711 \end{bmatrix} $$

The problem I have (with any combination of A), B) I), II)) is that $|u\rangle$ and $|v\rangle$ have an overlap of 1, so are equivalent. The reason is that the encoding of $v$ is equal to the encoding of $u$, just with a global phase added,

s.t. the fidelity (as computed by qiskit) for example is

$$ | \langle u|v\rangle |^2 = 1.0 $$

but $\text{dist_eucl}(u,v)$ is far from 0.

What encoding can I use that will produce distinct qubits for positive and negative real inputs?


I finally used the statepreparation described here For real vectors in the range $[-1,1]$ I obtain quantum distances corresponding to euclidian distances: quantum distance

  • $\begingroup$ The behaviour of Qubits is best described with vectors; but those vectors are probability amplitudes that describe a data register; they do not correspond to Euclidean Distance in any clear way; even when thinking about Qubits using a Bloch Sphere. How would your algorithm work calculate the distance between (1,0) and (5,0)? $\endgroup$ Sep 22, 2021 at 19:34
  • $\begingroup$ Undoubtedly this task can be done using a Quantum Computer...however this simplification of trying to encode a real vector as a single qubit likely will not lead anywhere. $\endgroup$ Sep 22, 2021 at 19:34

1 Answer 1


As you've noticed, global phase is a problem here. One option that you could pursue (I'm not claiming it's a good option) is to use a two-qubit encoding. Let's say you want to encode a vector $u\in\mathbb{C}^2$ (you asked about real, which is obviously a simplifying case) $$ u=\left[\begin{array}{c} ae^{i\theta_1} \\ be^{i\theta_2} \end{array}\right] $$ where $a,b\geq 0$. You could encode this as $$ \frac{1}{\sqrt{a^2+b^2}}\left[\begin{array}{c} a \\ be^{i(\theta_2-\theta_1)} \end{array}\right]\otimes \left[\begin{array}{c} \cos2\theta_1 \\ \sin2\theta_1 \end{array}\right] $$

In the case where your vectors are real, I believe this gives the same inner product as the original vector, up to the effect of the normalisation factor.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.