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

Successive use of same UCCDefault instance adds more passes #261

Closed
bachase opened this issue Feb 26, 2025 · 4 comments
Closed

Successive use of same UCCDefault instance adds more passes #261

bachase opened this issue Feb 26, 2025 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@bachase
Copy link
Collaborator

bachase commented Feb 26, 2025

Describe the bug
Each call to run on the same instance of UCCDefault1 adds additional passes:

self.add_map_passes(coupling_list)
self.pass_manager.append(
BasisTranslator(sel, target_basis=self.target_basis)
)
out_circuits = self.pass_manager.run(circuits)

To Reproduce
Run the following code

from ucc.transpilers.ucc_defaults import UCCDefault1
from qiskit import QuantumCircuit as QiskitCircuit

ucc_compiler = UCCDefault1()
circuit = QiskitCircuit(2)
circuit.h(0)
circuit.cx(0, 1)

for run in range(5):
    print(f"{run}: Num passes {len(ucc_compiler.pass_manager._tasks)}")
    custom_compiled_circuit = ucc_compiler.run(circuit)

gives output

0: Num passes 8
1: Num passes 9
2: Num passes 10
3: Num passes 11
4: Num passes 12

Expected behavior
I would expect the number of passes to remain fixed between runs, so an output of

0: Num passes 8
1: Num passes 8
2: Num passes 8
3: Num passes 8
4: Num passes 8
@bachase bachase added the bug Something isn't working label Feb 26, 2025
@bachase
Copy link
Collaborator Author

bachase commented Feb 26, 2025

@jordandsullivan and @Misty-W -- I stumbled on this while trying to understand more about the set of default passes. I'm not sure if its actually intended that the passes are modified here, but it seemed surprising on first review.

@jordandsullivan
Copy link
Collaborator

jordandsullivan commented Feb 26, 2025

So because the intended user interface is ucc.compile(circuit) (which initializes a fresh instance of UCCDefault1() on every call):

ucc/ucc/compile.py

Lines 53 to 56 in 1c1b6f1

compiled_circuit = UCCDefault1().run(
qiskit_circuit,
coupling_list=get_backend_connectivity(target_device),
)

we didn't consider successive calls to UCCDefault1().run() as a potential workflow, but I suppose we could separate the pass manager object and initialize it when calling UCCDefault1().run() rather than initalizing it with UCCDefault1(), as currently happens:

class UCCDefault1:
def __init__(self, local_iterations=1):
self.pass_manager = PassManager()

@bachase
Copy link
Collaborator Author

bachase commented Feb 26, 2025

I had stumbled on the non-ucc.compiler(circuit) path when following this part of the docs around a non-default pass.

Do we view UCCDefault1 as an equivalent of a PassManager, just with some specific defaults? If so, perhaps we could initialize all the passes in the initializer instead. This would require users to provide the coupling_list at the time its created.

If we did this way, then the approach linked in the docs for adding additional custom passes would work directly, and I think would match user expectation that an instance of UCCDefault1 starts with the default set of passes already.

@jordandsullivan
Copy link
Collaborator

Gotcha, yes I agree with that approach. I think it would be more intuitive for contributor-users.

@bachase bachase self-assigned this Feb 26, 2025
bachase added a commit that referenced this issue Feb 27, 2025
Addresses #261 by setting up the PassManager in the UCCDefault initializer rather than on each run. The second commit updates the corresponding examples in the docs to reflect current module structure, converts the examples to Sphix doctest to ensure examples we can automate checking they run properly with new code changes, and adds a doctest check to the Test workflow to do that automation.
@bachase bachase closed this as completed Feb 27, 2025
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