Closed JoelHBierman closed 2 years ago
UCCSD is a BlueprintCircuit based on the EvolvedOperator in Terra which means that the circuit can change and it does not need to be complete when constructed but can be setup/changed later. The 'random lines' yo added to read parameters do in fact cause it to do actions internally to allow it to pass back values, and it may need to build out aspects to do this. E.g. asking for num_qubits requires the EvolvedOperator to know that from the operators which it requires UCCSD to build etc. Anyway bottom line it seems that while everything could be built at the outset in your case, the operators, are not. But when EvolvedOperator has the operators it also sets up a qregs based on the number of qubits and without that the transpile is failing.
It does make sense to me that calling these attributes would have internal effects that change the UCCSD instance. By 'random lines' I mean that from a user perspective, it's not at all obvious that adding these lines would be necessary for utilizing functionality offered by other QuantumCircuit objects that are typically used for the same purposes. I essentially stumbled upon this workaround completely by accident. e.g. EfficientSU2, RealAmplitudes, ect. are also typically used as ansatzes for variational algorithms, but they do not behave this way. (Presumably because they are not EvolvedOperators.)
The things I am still unsure of are: 1. Whether this is a bug or the expected/intended behavior and 2. If it is a bug, whether or not there can be minimal changes made to the UCCSD code that makes using this functionality a bit more intuitive .
It is not good overall behavior on the part of the objects, I agree. Since you have the object fully built by the constructor it ought to be completely valid at that point and not fail transpilation etc. Yes, I have a change, that I was looking at this morning, after I wrote the above, that improves things so it works as one might expect and your example code passes without any of the "random" lines.
Great, thank you! Once that merges I will close this issue.
The way we normally do things is that the PR which fixes/implements an issue will auto-close the issue when the PR is merged. As you can see in the text above your comment ... linked a pull request 32 minutes ago that will close this issue
. If you want to try the code - though you can see test cases like your example. that fully build and then transpile, and check things are fine yourself - you can install from source here once that PR is merged (or make the small change locally in your code - its the one liner _ = self.operators
which calls that for the possible side-effect of building them out and as a by product knowing the number of qubits in the EvolvedOperator ansatz where they are set and the qregs is set also.
Environment
What is happening?
Calling
transpile(UCCSD_instance, backend=some_backend)
fails unless certain class attributes are first called. (e.g. I have testednum_parameters
,parameters
, andnum_qubits
, but there may be others.)How can we reproduce the issue?
Running the following code:
results in the following traceback and error message:
Un-commenting out any of the lines
ansatz.num_qubits
,ansatz.num_parameters
, oransatz.parameters
allows the program to transpile the circuit without error.What should happen?
Transpiling UCCSD should not require adding random lines that don't do anything except refer to some of its attributes.
Any suggestions?
No response