Skip to content

Several modules, e.g QBaysian, use a private attribute of QuantumCircuit #943

@woodsp-ibm

Description

@woodsp-ibm

I noticed the following in the QBayasian code where the use of a variable with the private naming ie leading underscore was flagged. As this is using what is normally considered implementation private and can be changed at any time it would be better to be relying on a supported public interface instead. (Raising an issue perhaps on qiskit to address this need if its not met elsewhere - which I kinda assume is the case due to the use of that private.)

if hasattr(circuit.layout, "_input_qubit_count"):
self.num_virtual_qubits = circuit.layout._input_qubit_count
else:
if pass_manager is None:
self.num_virtual_qubits = circuit.num_qubits
else:
circuit = pass_manager.run(circuit)
self.num_virtual_qubits = circuit.layout._input_qubit_count

Now I had to do a double take on the code snippet above as while it seemed to be doing a conditional check on the presence and accessing it in the if part - it goes ahead anyway and just accesses it in the else at the end. It seems on further research that the transpiler adds that so running the pass manager has the side-effect of creating it so it exists despite at first glance being wrong as its inside the else part. Some comments in the code would have been helpful - though to me anyway the use of a private member is rather suspect as the private variable is really an internal implementation detail that not being public and supported could be changed at any time.


Note: Code using the same private variable is present in several other files too where the same comment about its use applies.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions