question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when adding gate after circuit.initialize method

See original GitHub issue

Environment

  • Qiskit Terra version: 0.19.2
  • Python version: 3.9.6
  • Operating system: Windows 11

What is happening?

Failure to run quantum circuit after circuit.initialize() method and any gate, using either statevector or qasm simulator.

The circuit runs when only having an initialize method. But it fails when more gates are appended after the initialization.

How can we reproduce the issue?

beta = 0.3
alpha = np.sqrt(1 - abs(beta)**2)
qc = QuantumCircuit(1)
qc.initialize([alpha, beta], 0)
qc.h(0)
qc.measure_all()
qc.draw()

download

sv_sim = Aer.get_backend('statevector_simulator')
qasm_sim = Aer.get_backend('qasm_simulator')
execute(qc, qasm_sim).result().get_counts()

imageimage

What should happen?

I would imagine we can initialize a state and apply any gate we want to it. Here, this code block should get a Python dictionary of measurement counts.

Any suggestions?

The second param of initialize() method can also be a bit misleading, causing ambiguity when reading the doc, since one may natively think “qubits” as the number of qubits. But I think I get the intention now.

image

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
wyqian1027commented, Oct 19, 2022

Could you try upgrading your version of Terra, and seeing if the bug persists? You’ve got a pretty old version (0.19.2) compared to the current 0.22.0.

(For what it’s worth, I ran this:

import numpy as np
from qiskit import QuantumCircuit, Aer, execute

beta = 0.3
alpha = np.sqrt(1 - abs(beta)**2)
qc = QuantumCircuit(1)
qc.initialize([alpha, beta], 0)
qc.h(0)
qc.measure_all()
qc.draw()

sv_sim = Aer.get_backend('statevector_simulator')
qasm_sim = Aer.get_backend('qasm_simulator')
execute(qc, qasm_sim).result().get_counts()

with Terra 0.22.0 and Aer 0.11.0 and got

{'1': 197, '0': 827}

which looks about right to me.)

Thanks a million, I will do so right away!

1reaction
jakelishmancommented, Oct 19, 2022

Could you try upgrading your version of Terra, and seeing if the bug persists? You’ve got a pretty old version (0.19.2) compared to the current 0.22.0.

(For what it’s worth, I ran this:

import numpy as np
from qiskit import QuantumCircuit, Aer, execute

beta = 0.3
alpha = np.sqrt(1 - abs(beta)**2)
qc = QuantumCircuit(1)
qc.initialize([alpha, beta], 0)
qc.h(0)
qc.measure_all()
qc.draw()

sv_sim = Aer.get_backend('statevector_simulator')
qasm_sim = Aer.get_backend('qasm_simulator')
execute(qc, qasm_sim).result().get_counts()

with Terra 0.22.0 and Aer 0.11.0 and got

{'1': 197, '0': 827}

which looks about right to me.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Figure of large circuit with initialize gives an error on rendering ...
I am trying to initialize a quantum circuit and draw the circuit using the Matplotlib backend: size = 4 q = QuantumRegister(size, name='q')...
Read more >
Error when attempting to visualize controlled hadamard gate ...
The error refers to a "Unitary" method. This could be the unitary_simulator / UnitarySimulator the tutorial used versus the one I imported.
Read more >
qiskit.extensions.Initialize — Qiskit 0.24.1 documentation
Call to create a circuit with gates that take the desired vector to zero. inverse (). Invert this instruction. is_parameterized ().
Read more >
Build, edit, and inspect quantum circuits
You can also double-click a gate to access the Edit operation panel, and set the conditional parameters that way. Add a control modifier¶....
Read more >
Models — Qibo 0.1.9 documentation
Creates a copy of the circuit with noise gates after each gate. ... Initialize the Grover algorithm with the superposition and Grover ancilla....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found