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

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

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.

  • 1
    $\begingroup$ 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. $\endgroup$ – Lena May 10 at 12:29
  • $\begingroup$ 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>. $\endgroup$ – Ali Javadi May 10 at 12:35
  • $\begingroup$ 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 :) $\endgroup$ – Lena May 10 at 12:38
  • $\begingroup$ @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? $\endgroup$ – Weather Report May 11 at 7:21
  • $\begingroup$ @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 :) $\endgroup$ – Lena May 11 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

  • $\begingroup$ 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 $\endgroup$ – Weather Report May 11 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}⟩ "


+ (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.

  • $\begingroup$ 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? $\endgroup$ – Weather Report May 12 at 6:40
  • $\begingroup$ 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? $\endgroup$ – Frank May 12 at 8:26

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.