Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect representation of state vector with state.draw('latex') #13314

Open
TerraVenil opened this issue Oct 13, 2024 · 1 comment
Open

Incorrect representation of state vector with state.draw('latex') #13314

TerraVenil opened this issue Oct 13, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@TerraVenil
Copy link

TerraVenil commented Oct 13, 2024

Environment

  • Qiskit version: 1.2.4
  • Python version: Python 3.12.7
  • Operating system: Windows 11 23H2

What is happening?

Wrong representation of the state vector after circuit execution when using state.draw('latex') function.

How can we reproduce the issue?

When I run the following circuit

from qiskit import QuantumCircuit
from qiskit.circuit.library.standard_gates import XGate
from qiskit_aer import StatevectorSimulator

num_qubits=4
circuit = QuantumCircuit(num_qubits)
cccx = XGate().control(num_ctrl_qubits=3, label=None, ctrl_state='100')
circuit.x(2)
circuit.compose(cccx, range(num_qubits), inplace=True)
circuit.draw('mpl')

Image

I do expect after state vector simulation to get ∣1100⟩

job = StatevectorSimulator().run(circuit)
state = job.result().get_statevector()
state.draw('latex')

but instead ∣0100⟩.

What should happen?

So the community suggested trying another approach to display a state vector

from qiskit.quantum_info import Statevector
statevector = Statevector(circuit)
statevector.draw("latex") 

and it works as expected ∣1100⟩.

Any suggestions?

Probably state.draw('latex') has an issue with displaying a state vector.

@TerraVenil TerraVenil added the bug Something isn't working label Oct 13, 2024
@TerraVenil TerraVenil changed the title Incorret state vector represendation by state.draw('latex') Incorret represendation of state vector with state.draw('latex') Oct 13, 2024
@TerraVenil TerraVenil changed the title Incorret represendation of state vector with state.draw('latex') Incorrect representation of state vector with state.draw('latex') Oct 15, 2024
@TrangOul
Copy link

TrangOul commented Oct 16, 2024

This is not an issue with displaying, but with the underlying value. Regardless of display method, the same value is displayed.
Using the variables defined above:

In [31]: state
Out[31]: Statevector([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, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], dims=(2, 2, 2, 2))

In [32]: statevector
Out[32]: Statevector([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, 0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j], dims=(2, 2, 2, 2))

In [35]: state.draw('latex_source')
Out[35]: ' |0100\\rangle'

In [36]: statevector.draw('latex_source')
Out[36]: ' |1100\\rangle'

And results for other inputs:

def generate_bits(n: int):
	yield from (bin(i)[2:].rjust(n, '0') for i in range(2**n))

for input_bits in generate_bits(3):
	num_qubits = 4
	circuit = QuantumCircuit(num_qubits)
	cccx = XGate().control(num_ctrl_qubits=3, label=None, ctrl_state=input_bits)
	circuit.x(2)
	circuit.compose(cccx, range(num_qubits), inplace=True)
	
	job = StatevectorSimulator().run(circuit)
	state = job.result().get_statevector()
	statevector = Statevector(circuit)
	
	print(input_bits)
	print(f"circuit state: {statevector.draw("latex_source")}")
	print(f"result of run: {state.draw('latex_source')}")
	print()
000
circuit state:  |0100\rangle
result of run:  |0100\rangle

001
circuit state:  |0100\rangle
result of run:  |0100\rangle

010
circuit state:  |0100\rangle
result of run:  |1100\rangle

011
circuit state:  |0100\rangle
result of run:  |1100\rangle

100
circuit state:  |1100\rangle
result of run:  |0100\rangle

101
circuit state:  |0100\rangle
result of run:  |0100\rangle

110
circuit state:  |0100\rangle
result of run:  |0100\rangle

111
circuit state:  |0100\rangle
result of run:  |0100\rangle

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants