Search code examples
pythonvisualizationqiskit

QisKit: Difference on Statevector / No Difference on Vizualization


Why is there a difference in the state vectors, but there is no difference in the visualizations?
Is there any other visualization to show the differences?

qiskit: 0.23.3
python: 3.8

circuit-1 + statevector:
q_0: 
[1.+0.j 0.+0.j] ==> +1

circuit-2 + statevector
     ┌───┐┌───┐┌───┐┌───┐┌───┐
q_0: ┤ X ├┤ H ├┤ X ├┤ H ├┤ X ├
     └───┘└───┘└───┘└───┘└───┘
[-1.+0.j -0.-0.j] ==> -1

Code example

import numpy as np
from qiskit import *
import qiskit.tools.visualization as vis
import matplotlib. pyplot as plt
from qiskit import Aer

circ = QuantumCircuit(1)

circ.x(0)
circ.h(0)
circ.x(0)
circ.h(0)
circ.x(0)

backend = Aer.get_backend('statevector_simulator')
result = execute(circ, backend).result()
statevector = result.get_statevector()

vis.plot_bloch_multivector(statevector)
vis.plot_state_city(statevector)
vis.plot_state_qsphere(statevector)
vis.plot_state_paulivec(statevector)
circ.draw(output='mpl')
print(np.around(statevector,5))
plt.show()

The visualizations of the the two statevectors are equal!! example 1 example 2


Solution

  • It seems like a bug to me (I reported here)

    In any case, I recommend you Kaleidoscope to visualize states. It has interactive graphs. Here is your example (refactor for shortness):

    from qiskit import QuantumCircuit
    from qiskit.quantum_info import Statevector
    from kaleidoscope import qsphere
    
    circ = QuantumCircuit(1)
    
    circ.x(0)
    circ.h(0)
    circ.x(0)
    circ.h(0)
    circ.x(0)
    
    state = Statevector.from_instruction(circ)
    qsphere(state)
    

    enter image description here

    Compare the color with the other example:

    circ = QuantumCircuit(1)
    
    state = Statevector.from_instruction(circ)
    qsphere(state)
    

    enter image description here

    The difference in color is the phase. In the first case is π.