Skip to content

Commit

Permalink
Merge pull request #40 from thisac/fix/qir-load-midfunc-return
Browse files Browse the repository at this point in the history
Fix QIR load mid-function return
  • Loading branch information
thisac authored Jul 18, 2023
2 parents 12893b4 + 98d7609 commit 97f20a7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion dwave/gate/qir/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ def _module_to_circuit(module: Module, circuit: Optional[Circuit] = None) -> Cir
meas_bits = []

for func in module.functions:
ret_set = False
for block in func.basic_blocks:
# if return has been set, then ignore rest of blocks
if ret_set:
break

for instr in block.instructions:
if instr.opcode is Opcode.RET:
if instr.opcode == Opcode.RET:
# break block on return code
ret_set = True
break

if instr.opcode == Opcode.CALL:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fixes:
- Fixes loading QIR scripts with a return mid-function.
32 changes: 32 additions & 0 deletions tests/test_qir/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,35 @@ def test_load_QIR_into_circuit(self):
circuit.append(ops.Z(circuit.qubits[0]))

assert [op.name for op in circuit.circuit] == ["X", "Y", "Z"]

def test_early_ret(self):
"""Test setting a return in an earlier block."""
qir_string = inspect.cleandoc(
r"""
; ModuleID = 'Citrus'
source_filename = "Citrus"
%Qubit = type opaque
define void @main() {
entry:
call void @__quantum__rt__initialize(i8* null)
call void @__quantum__qis__x__body(%Qubit* null)
ret void
body: ; preds = %entry
call void @__quantum__qis__y__body(%Qubit* null)
ret void
}
declare void @__quantum__rt__initialize(i8*)
declare void @__quantum__qis__x__body(%Qubit*)
declare void @__quantum__qis__y__body(%Qubit*)
"""
)
circuit = Circuit(2)

circuit = load_qir_string(qir_string, circuit=circuit)

assert len(circuit.circuit) == 1
assert [op.name for op in circuit.circuit] == ["X"]

0 comments on commit 97f20a7

Please sign in to comment.