Skip to content

Commit 5cc1c31

Browse files
committed
test with cycles in decomp graph
1 parent 9da30b4 commit 5cc1c31

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

frontend/test/lit/test_decomposition.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,5 +1136,64 @@ def c():
11361136
# CHECK-SAME: target_gate = "SingleParam{x:[f64]}{reg:1}{}"
11371137
test_multiple_rules_chained_and_branch()
11381138

1139+
def test_with_cycles():
1140+
"""
1141+
Tests for when the decomposition rules have cycles, i.e. graph looks like
1142+
A
1143+
|
1144+
+--+--+
1145+
| ^
1146+
v |
1147+
B---> C
1148+
"""
1149+
1150+
@qp.register_resources(lambda reg: {SingleParam(x=Float, reg=Wire[1]): 1})
1151+
def ruleAB(reg):
1152+
SingleParam(x=0.1, reg=reg[0])
1153+
1154+
@qp.register_resources(
1155+
lambda x, reg: {CompilableData(a="a", b="b", thing="thing", wires=Wire[1]): 1}
1156+
)
1157+
def ruleBC(x, reg):
1158+
CompilableData(a="a", b="b", thing="thing", wires=reg[0])
1159+
1160+
@qp.register_resources(lambda a, b, thing, wires: {NoParams(reg=Wire[2]): 1})
1161+
def ruleCA(a, b, thing, wires):
1162+
NoParams(reg=[0, 1])
1163+
1164+
with qp.decomposition.local_decomps():
1165+
qp.add_decomps(NoParams, ruleAB)
1166+
qp.add_decomps(SingleParam, ruleBC)
1167+
qp.add_decomps(CompilableData, ruleCA)
1168+
1169+
@qp.qjit(capture=True, target="mlir")
1170+
@qp.qnode(qp.device("null.qubit", wires=3))
1171+
def c():
1172+
NoParams(reg=[0, 1])
1173+
NoParams(reg=[0, 1, 2])
1174+
return qp.state()
1175+
1176+
print(c.mlir)
1177+
1178+
# CHECK: func.func public @c()
1179+
# CHECK: qref.operator "NoParams"
1180+
# CHECK: func.func private @"__builtin_ruleAB_NoParams{}{reg:2}{}"
1181+
# CHECK-SAME: resources = {operations = {
1182+
# CHECK-SAME: "SingleParam{x:[f64]}{reg:1}{}" = 1 : i64
1183+
# CHECK-SAME: target_gate = "NoParams{}{reg:2}{}"
1184+
# CHECK: func.func private @"__builtin_ruleBC_SingleParam{x:[f64]}{reg:1}{}"
1185+
# CHECK-SAME: resources = {operations = {
1186+
# CHECK-SAME: "CompilableData{}{wires:1}{a:a,b:b,thing:thing}" = 1 : i64
1187+
# CHECK-SAME: target_gate = "SingleParam{x:[f64]}{reg:1}{}"
1188+
# CHECK: func.func private @"__builtin_ruleCA_CompilableData{}{wires:1}{a:a,b:b,thing:thing}"
1189+
# CHECK-SAME: resources = {operations = {
1190+
# CHECK-SAME: "NoParams{}{reg:2}{}" = 1 : i64
1191+
# CHECK-SAME: target_gate = "CompilableData{}{wires:1}{a:a,b:b,thing:thing}"
1192+
# CHECK: func.func private @"__builtin_ruleAB_NoParams{}{reg:3}{}"
1193+
# CHECK-SAME: resources = {operations = {
1194+
# CHECK-SAME: "SingleParam{x:[f64]}{reg:1}{}" = 1 : i64
1195+
# CHECK-SAME: target_gate = "NoParams{}{reg:3}{}"
1196+
test_with_cycles()
1197+
11391198

11401199
test_lowering_time_rules()

0 commit comments

Comments
 (0)