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

Circuit is failed to visualize or execute when adding a cx gate with label=1 #8036

Closed
weucode opened this issue May 7, 2022 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@weucode
Copy link

weucode commented May 7, 2022

Informations

  • Qiskit version:0.36.1
  • Python version:3.7.4
  • Operating system:Windows

What is the current behavior?

Assigning an Integer value 1 to the argument label of cx gate will cause abnormal behavior.

Steps to reproduce the problem

If I assign an Integer value 1 to the argument label, Qiskit fails to draw the circuit and throws an AttributeError. It can be reproduced by running the following test case:

from qiskit import QuantumCircuit


raw_circuit = QuantumCircuit(2)
raw_circuit.cx(0, 1, 1)
print(raw_circuit.draw())

The error message is shown below:

  File "D:\Anaconda3\lib\site-packages\qiskit\visualization\text.py", line 75, in top
    ret = self.top_format % self.top_connect.center(self.width, self.top_pad)
AttributeError: 'int' object has no attribute 'center'

The code snippet to trigger this error is in the Bullet class of text.py as follows:

if label and bottom:
    self.bot_connect = label
elif label:
    self.top_connect = label

If the argument label is set to be more than 1, it will assign the value of label to self.top_connect, but if label is set to be 0, it will not do this.After this assignment, once the program try to visit the attribute 'center' in an 'int' type, it will wrong.

In addition, when running the compiled circuit as shown the following test case, Qiskit outputs an empty result.

from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator


raw_circuit = QuantumCircuit(2)
raw_circuit.cx(0, 1, 1)
raw_circuit.measure_all()
simulator = QasmSimulator()
compiled_circuit = transpile(raw_circuit, simulator)
job = simulator.run(compiled_circuit, shots=1000)
result = job.result()
print(result.get_counts(compiled_circuit))

A QiskitError is thrown as below:

Simulation failed and returned the following error message:
ERROR: Failed to load qobj: Unable to cast Python instance to C++ type (compile in debug mode for details)
Traceback (most recent call last):
File "test.py", line 12, in
print(result.get_counts(compiled_circuit))
File "D:\Anaconda3\lib\site-packages\qiskit\result\result.py", line 280, in get_counts
exp = self._get_experiment(key)
File "D:\Anaconda3\lib\site-packages\qiskit\result\result.py", line 391, in _get_experiment
raise QiskitError('Data for experiment "%s" could not be found.' % key)
qiskit.exceptions.QiskitError: 'Data for experiment "circuit-2" could not be found.'

After I debugged the whole process, I find that it may be occurred when calling an extra cyphon code, which try to execute the cx gate.

Suggested solutions

Maybe a type check for argument label will be helpful.

@weucode weucode added the bug Something isn't working label May 7, 2022
@jakelishman jakelishman transferred this issue from Qiskit/qiskit-metapackage May 10, 2022
@jakelishman
Copy link
Member

Thanks for the report! By coincidence, we've actually merged the type checking you've suggested in #7671, which will be released as part of Qiskit Terra 0.21 in around a couple of months.

The output of your example now is:

>>> from qiskit import QuantumCircuit
>>> raw_circuit = QuantumCircuit(2)
>>> raw_circuit.cx(0, 1, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jake/code/qiskit/terra/qiskit/circuit/quantumcircuit.py", line 3706, in cx
    CXGate(label=label, ctrl_state=ctrl_state), [control_qubit, target_qubit], []
  File "/Users/jake/code/qiskit/terra/qiskit/circuit/library/standard_gates/x.py", line 186, in __init__
    super().__init__(
  File "/Users/jake/code/qiskit/terra/qiskit/circuit/controlledgate.py", line 95, in __init__
    super().__init__(name, num_qubits, params, label=label)
  File "/Users/jake/code/qiskit/terra/qiskit/circuit/gate.py", line 38, in __init__
    super().__init__(name, num_qubits, 0, params, label=label)
  File "/Users/jake/code/qiskit/terra/qiskit/circuit/instruction.py", line 91, in __init__
    raise TypeError("label expects a string or None")
TypeError: label expects a string or None

@weucode
Copy link
Author

weucode commented May 11, 2022

Thanks for your reply!I still have no idea about why it failed in simulation step,can you give me some advice?

@jakelishman
Copy link
Member

The simulation likely succeeded, except you asked for the counts and there's no measure instructions in your circuit. There's only counts if you measure something into the classical bits. If you do something like

qc = QuantumCircuit(2, 2)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])

and then do your transpilation and simulator.run call, I think you'll get back what you expect.

@weucode
Copy link
Author

weucode commented May 12, 2022

I'm very sorry that I gave the wrong program,the real program that will cause QiskitError is as follows:

from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator


raw_circuit = QuantumCircuit(2)
raw_circuit.cx(0, 1, 1)
raw_circuit.measure_all()
simulator = QasmSimulator()
compiled_circuit = transpile(raw_circuit, simulator)
job = simulator.run(compiled_circuit, shots=1000)
result = job.result()
print(result.get_counts(compiled_circuit))

@jakelishman
Copy link
Member

You should remove the incorrect 1 label from your cx call - the cx should be raw_circuit.cx(0, 1); the second 1 is becoming the label, but as you said at the top, that should be a string (or just absent - it's not really used). With that fixed, I don't get any errors from your code.

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