# Inverse of Statevector.from_label in Qiskit

In Qiskit I can construct a computational basis statevector in the following fashion

from qiskit.quantum_info import Statevector
sv = Statevector.from_label('10')
print(sv.data)


which gives [0.+0.j 0.+0.j 1.+0.j 0.+0.j] . If I have the data for a statevector in the computation basis, is there a built-in way to return a corresponding string? For [0.+0.j 0.+0.j 1.+0.j 0.+0.j] I should get back '10'. More generally, if the state is not in the computational basis it would be nice to get a linear combination of the basis states.

• Hi, just a quick question, when I use the exact same code, I have [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j], not what you put on your question. Are you sure about your array? From the looks of it it would be a statevector describing a 2-qubits state, not 4.
– Lena
Commented May 10, 2021 at 12:29
• Yes the state corresponding to string '1010' is the vector in @Lena's comment. This is a 4-qubit state. It means qubit 3 in state |1>, qubit 2 in state |0>, qubit 1 in state |1>, and qubit 0 in state |0>. Commented May 10, 2021 at 12:35
• By the way, by looking at it, the array you have in your question is the array you would get with the label '10', which is the binary representation of 2, which is the index where the 1 is positioned on your array. So if you want to get back the string from the array, all you have to do is get the index of the position in the array and convert it into a binary string :)
– Lena
Commented May 10, 2021 at 12:38
• @Lena Yes, sorry, there was a mistake. Lena, yes I agree that the computation to get the binary representation is not difficult. Still I was wondering if there is a built-in way to to it. Also it would be nice to get more general case with linear combinations. If such a feature is not available, how can I propose to add it to Qiskit? Commented May 11, 2021 at 7:21
• @WeatherReport I don't know if there is a build-in function to do what you want, from my knowledge there isn't but I might be mistaken on this. If you want you can open an issue here on Github, explain what you'll like to have, and then do a PR and everything :)
– Lena
Commented May 11, 2021 at 7:49

You can just use the constructor of the Statevector class.

sv1 = Statevector.from_label('1010')  # construct Statevector from string label

sv2 = Statevector(sv1.data)   # construct Statevector from numpy array

sv1 == sv2


returns True

• Thank you, I see what you mean. However what I want to get back is the string 1010 (or 10 in the corrected version of the question) from the sv.data Commented May 11, 2021 at 7:18

The output is a bit ugly but I think basically does what you want:

import numpy as np
from qiskit.quantum_info import random_statevector
sv = random_statevector(4)

s = ""
for state, amp in enumerate(sv.data):
if not np.isclose(amp, 0):
s += f"+ ({amp:.2f})|{state:0{sv.num_qubits}b}⟩ "
print(s)


output:

+ (0.34-0.22j)|00⟩ + (-0.53-0.34j)|01⟩ + (0.43-0.24j)|10⟩ + (-0.12-0.42j)|11⟩


You can make this smarter and neater if you need.

• Thanks, you code shows that it is even easier than I have expected. Still I think this is the functionality better to be built-in. What do yo think about this PR github.com/Qiskit/qiskit-terra/pull/6154 pointed out in the comments here? Commented May 12, 2021 at 6:40
• I agree it would be a nice feature. I don't know how long it will take for that PR to be merged but it probably won't be for a while. What do you want this for? Will the output be used in a document, or be fed into another program, or just be read by you? Commented May 12, 2021 at 8:26