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

Runtime error when using KAKDecomposition on Circboxes #1553

Open
Bennybenassius opened this issue Aug 27, 2024 · 2 comments
Open

Runtime error when using KAKDecomposition on Circboxes #1553

Bennybenassius opened this issue Aug 27, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Bennybenassius
Copy link

Bennybenassius commented Aug 27, 2024

Ilan and I have found this issue during random testing:

When running:

from pytket import Circuit, Qubit
from pytket.circuit import CircBox
from pytket.passes import *

subcirc_0 = Circuit(0, "subcirc_0")
qbit_0 = Qubit("qbit_0", 0)
subcirc_0.add_qubit(qbit_0)
qbit_1 = Qubit("qbit_1", 0)
subcirc_0.add_qubit(qbit_1)
subcirc_0.X(qbit_0)
circbox_subcirc_0 = CircBox(subcirc_0)

subcirc_1 = Circuit(0, "subcirc_1")

qreg_0 = subcirc_1.add_q_register("qreg_0",1)
qreg_1 = subcirc_1.add_q_register("qreg_1",1)
subcirc_1.CZ(qreg_0[0], qreg_1[0])
circbox_subcirc_1 = CircBox(subcirc_1)

main_circ = Circuit(4, 2, "main_circ")

# Applying gates 
main_circ.add_gate(circbox_subcirc_0,[0, 1])
# main_circ.CX(0, 2).X(0)
main_circ.add_gate(circbox_subcirc_1,[0, 1])
main_circ.CX(1, 2)

KAKDecomposition().apply(main_circ)

Throws a runtime error:

 RuntimeError: Cannot obtain matrix from op: CircBox 

This error seems to only occur when the Circboxes are right next to each other. Upon uncommenting the line # main_circ.CX(0, 2).X(0), the code runs without an issue and simply leaves the Circboxes untouched. This bug also disappears when DecomposeBoxes is used on the Circuit first.

@cqc-alec cqc-alec added the bug Something isn't working label Sep 3, 2024
@CalMacCQ
Copy link
Contributor

CalMacCQ commented Sep 5, 2024

Hmm I find this surprising that the error only manifests when the two CircBox(es) are next to one another. As you pointed out if we put gates in between then the KAKDecomposition pass can be applied without issue.

The subcircuits here are pure quantum so the subcircuit unitary is well defined.

We can do

circbox_subcirc_0.get_unitary()

and

circbox_subcirc_1.get_unitary()

without issue.

In the default_compilation_pass for the different backends we always call DecomposeBoxes prior to doing local rewrites like KAKDecomposition or FullPeepholeOptimise. However its understandable that the beahviour exhibited above is confusing. If indeed DecomposeBoxes is needed then this should ideally be clear from the error message.

Any ideas @cqc-alec ?

@cqc-alec
Copy link
Collaborator

cqc-alec commented Sep 5, 2024

I would expect KAKDecomposition to leave all CircBoxes alone and optimize around them; this is clearly a bug.

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

3 participants