Skip to content

QuantumCircuit.copy(name=?) allows for any object to be used as name #10487

@MattePalte

Description

@MattePalte

Environment

  • Qiskit Terra version: 0.43.1 meta package, terra 0.24.1
  • Python version: 3.10
  • Operating system: docker continuumio/miniconda3

What is happening?

Creating a circuit via copy allows to give an invalid name (not a string), that is then checked only when the circuit is transpiled.
In this case the copy() api raises no error when passing a circuit as name (see API: copy)

How can we reproduce the issue?

Run this python script:

from qiskit import QuantumCircuit
from qiskit.compiler import transpile
qc_output = QuantumCircuit(2, 2)
input_circuit = QuantumCircuit(2, 2)
input_circuit.measure([0, 1], [0, 1])
qc_output = qc_output.compose(input_circuit, qubits=range(2))
final_circuit = qc_output.copy(qc_output)
final_circuit.measure([0, 1], [0, 1])
print(final_circuit.draw())

print("Circuit name: ", final_circuit.name)

transpile(final_circuit, optimization_level=0)

Produces this output and error:

     ┌─┐   ┌─┐
q_0: ┤M├───┤M├───
     └╥┘┌─┐└╥┘┌─┐
q_1: ─╫─┤M├─╫─┤M├
      ║ └╥┘ ║ └╥┘
c: 2/═╩══╩══╩══╩═
      0  1  0  1
Circuit name:       ┌─┐
q_0: ┤M├───
     └╥┘┌─┐
q_1: ─╫─┤M├
      ║ └╥┘
c: 2/═╩══╩═
      0  1
Traceback (most recent call last):
  File "myfile.py", line 13, in <module>                       transpile(final_circuit, optimization_level=0)
  File ".../qiskit/compiler/transpiler.py", line 380, in transpile                    _serial_transpile_circuit(
  File ".../qiskit/compiler/transpiler.py", line 462, in _serial_transpile_circuit    result = pass_manager.run(circuit, callback=callback, output_name=outpu
t_name)                                                                      File ".../qiskit/transpiler/passmanager.py", line 537, in run                       return super().run(circuits, output_name, callback)
  File ".../qiskit/transpiler/passmanager.py", line 231, in run                       return self._run_single_circuit(circuits, output_name, callback)
  File ".../qiskit/transpiler/passmanager.py", line 292, in _run_single_circuit       result = running_passmanager.run(circuit, output_name=output_name, call
back=callback)                                                               File ".../qiskit/transpiler/runningpassmanager.py", line 127, in run                circuit = dag_to_circuit(dag, copy_operations=False)
  File ".../qiskit/converters/dag_to_circuit.py", line 58, in dag_to_circuit          circuit = QuantumCircuit(
  File ".../qiskit/circuit/quantumcircuit.py", line 260, in __init__                  raise CircuitError(
qiskit.circuit.exceptions.CircuitError: 'The circuit name should be a strin
g (or None to auto-generate a name).'

What should happen?

I would expect the copy method to check the name of the circuit, and raise an error if it is not a string, whereas now it is allowed without any check (see here).

Even this is allowed:

...
class AnyObject(object):
    def __init__(self, field):
        self.field = field
new_circuit = qc_output.copy(name=AnyObject("new_circuit"))
print(new_circuit.name)

Outputting:

...
<__main__.AnyObject object at 0x7f7c98809ab0>

Any suggestions?

I think the copy method should check the name of the circuit, and raise an error if it is not a string, so that the error is more precise on the line of the copy() call rather than the transpile().

Metadata

Metadata

Labels

bugSomething isn't workinggood first issueGood for newcomers

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions