1
$\begingroup$

Suppose I instantiate a state with 4 qubits in their $|0\rangle$ state with qutip:

from qutip import basis, tensor

state = tensor(*[basis(2,0)]*4)

The output will look like this:

Quantum object: dims=[[2, 2, 2, 2], [1, 1, 1, 1]], shape=(16, 1), type='ket', dtype=Dense
Qobj data =
[[1.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]]

For a machine, this is great. But now I can't tell which component is which. For example, does the second element correspond to $|0001\rangle$? What ordering does QuTip take when writing the vector notation of these states? Does it go from the lowest $|0000\rangle$ to highest $|1111\rangle$ or does it take another approach? Is there a way to print the state in a more human-readable format?


Update:

Apparently, the output is in increasing order because if I do

tensor(basis(2,0),basis(2,1),basis(2,0),basis(2,0))

The output is

Quantum object: dims=[[2, 2, 2, 2], [1, 1, 1, 1]], shape=(16, 1), type='ket', dtype=Dense
Qobj data =
[[0.]
 [0.]
 [0.]
 [0.]
 [1.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]
 [0.]]

Which is on the fourth place, as it would be expected if it was in increasing order.

$\endgroup$

2 Answers 2

1
$\begingroup$

The closest thing I got from a human-readable response was this:

import tabulate
from qutip import basis, tensor

ψ = tensor(*[basis(2,0)]*4)

rows = []
for j in range(2**4):
    if ψ[j][0] != 0:
        rows.append({
            "state": f"{j:04b}",
            "amplitude": f"{np.real(ψ[j][0])} + j{np.imag(ψ[j][0])}"
        })

print(tabulate.tabulate(rows, headers="keys"))

Should output:

  state  amplitude
-------  -----------
   0000  1.0 + j0.0

I will leave the question open if someone has a better approach.

$\endgroup$
0
$\begingroup$

Suppose we are talking about computational basis states only. For $n$-qubits states, these are vectors with dimension $2^n$ and are composed of one "1" component whereas others are zero. A position of "1" specify which basis state is represented by the vector. If "1" is on $i$th position, then the vector corresponds to state $|i-1\rangle$. Number $i-1$ has to be converted to binary.

For example, if three-qubit state is written as $(0\,0\,0\,0\,1\,0\,0\,0)$, then "1" is on fifth place ($i=5$), hence it is the state corresponding to number 4, in binary 100, so the equivalent basis state is $|100\rangle$.

And here is Python code allowing to convert a basis state expressed as an array to human readable form:

import numpy as np
import math as m
import cmath as cm

#general state in vector form to Dirac notation
def vectorToDirac (state):
    result = ''
    state = state / np.linalg.norm(state,2) #state normalization
    stateDim = int(m.log(len(state),2))
    
    k = 0
    for i in range(0,len(state)):
        if state[i] <> 0:   
            if k > 0: result = result + ' + ' 
            
            result = result +  str(abs(state[i])) #abs. of probability aplitude
            
            #basis state phase            
            if cm.phase(state[i]) <> 0: result = result + ' EXP[' + str(cm.phase(state[i])) + 'i]'
            
            result = result + '|' + bin(i).replace('0b','').zfill(stateDim) + '>'                 
            
            #go to new line to have better readability of the output
            k = k + 1
            if k % 2 == 0: result = result + '\n'
                
    return result

#print basis state in readable form
qubits = 3 # number of qubits
basisStates = 2**qubits

state = np.zeros([1,2**qubits])

#print all basis state in vector and readable forms
for i in range(0,basisStates):     
    if i == 0:
        state[0][0] = 1
    else:
        state[0][i - 1] = 0
        state[0][i] = 1
    print(str(state) + ' => ' + vectorToDirac(state[0]))   

#normalize and print general state (from vector to Dirac notation)
print('\n')
state = np.array([-1+1j,1,1-1j,1+1j])
print vectorToDirac(state)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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