Skip to content

Commit

Permalink
Issue 92 Classical Ordering (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asa-Kosto-QTM authored Jan 20, 2024
1 parent d627cb3 commit 8e52dea
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pytket/phir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def main() -> None:
for file in args.qasm_files:
print(f"Processing {file}") # noqa: T201
c = circuit_from_qasm(file)
tket_opt_level = int(args.tk)
tket_opt_level = int(args.tket_opt_level)
rc = rebase_to_qtm_machine(c, args.machine, tket_opt_level)
qasm = circuit_to_qasm_str(rc, header="hqslib1")
circ = circuit_from_qasm_str(qasm)
Expand Down
2 changes: 1 addition & 1 deletion pytket/phir/sharding/sharder.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def _resolve_shard_dependencies(
"...adding shard dep %s -> WAW", self._bit_written_by[bit_written]
)
depends_upon.add(self._bit_written_by[bit_written])
elif bit_written in self._bit_read_by:
if bit_written in self._bit_read_by:
logger.debug(
"...adding shard dep %s -> WAR", self._bit_read_by[bit_written]
)
Expand Down
11 changes: 11 additions & 0 deletions tests/data/qasm/classical_ordering.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
OPENQASM 2.0;
include "hqslib1.inc";

qreg q[1];
creg a[4];
creg b[4];
creg c[4];
a = 3;
b = a;
c = (a - b);
a = (a << 1);
95 changes: 93 additions & 2 deletions tests/test_sharder.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_simple_conditional(self) -> None:
assert shards[3].qubits_used == {circuit.qubits[0]}
assert shards[3].bits_written == {circuit.bits[0]}
assert shards[3].bits_read == {circuit.bits[0]}
assert shards[3].depends_upon == {shards[0].ID, shards[1].ID}
assert shards[3].depends_upon == {shards[0].ID, shards[1].ID, shards[2].ID}
assert len(shards[3].sub_commands.items()) == 1
s2_qubit, s2_sub_cmds = next(iter(shards[3].sub_commands.items()))
assert s2_qubit == circuit.qubits[0]
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_classical_hazards(self) -> None:
assert shards[3].qubits_used == set()
assert shards[3].bits_written == {circuit.bits[0]}
assert shards[3].bits_read == {circuit.bits[0]}
assert shards[3].depends_upon == {shards[0].ID}
assert shards[3].depends_upon == {shards[0].ID, shards[2].ID}

# shard 4: [] if(c[2]==1) c[0]=1;
assert shards[4].primary_command.op.type == OpType.Conditional
Expand Down Expand Up @@ -307,3 +307,94 @@ def test_with_big_gate(self) -> None:
assert len(shards[1].sub_commands) == 0
assert shards[1].qubits_used == {circuit.qubits[3]}
assert shards[1].bits_written == {circuit.bits[0]}

def test_classical_ordering_breaking_circuit(self) -> None:
circuit = get_qasm_as_circuit(QasmFile.classical_ordering)
sharder = Sharder(circuit)
shards = sharder.shard()

assert len(shards) == 4

# shard 0
assert shards[0].primary_command.op.type == OpType.SetBits
assert len(shards[0].sub_commands.items()) == 0
assert shards[0].qubits_used == set()
assert shards[0].bits_written == {
circuit.bits[0],
circuit.bits[1],
circuit.bits[2],
circuit.bits[3],
}
assert shards[0].bits_read == {
circuit.bits[0],
circuit.bits[1],
circuit.bits[2],
circuit.bits[3],
}
assert shards[0].depends_upon == set()

# shard 1
assert shards[1].primary_command.op.type == OpType.CopyBits
assert len(shards[1].sub_commands.items()) == 0
assert shards[1].qubits_used == set()
assert shards[1].bits_written == {
circuit.bits[4],
circuit.bits[5],
circuit.bits[6],
circuit.bits[7],
}
assert shards[1].bits_read == {
circuit.bits[0],
circuit.bits[1],
circuit.bits[2],
circuit.bits[3],
circuit.bits[4],
circuit.bits[5],
circuit.bits[6],
circuit.bits[7],
}
assert shards[1].depends_upon == {shards[0].ID}

# shard 2
assert shards[2].primary_command.op.type == OpType.ClassicalExpBox
assert len(shards[2].sub_commands.items()) == 0
assert shards[2].qubits_used == set()
assert shards[2].bits_written == {
circuit.bits[8],
circuit.bits[9],
circuit.bits[10],
circuit.bits[11],
}
assert shards[2].bits_read == {
circuit.bits[0],
circuit.bits[1],
circuit.bits[2],
circuit.bits[3],
circuit.bits[4],
circuit.bits[5],
circuit.bits[6],
circuit.bits[7],
circuit.bits[8],
circuit.bits[9],
circuit.bits[10],
circuit.bits[11],
}
assert shards[2].depends_upon == {shards[0].ID, shards[1].ID}

# shard 2
assert shards[3].primary_command.op.type == OpType.ClassicalExpBox
assert len(shards[3].sub_commands.items()) == 0
assert shards[3].qubits_used == set()
assert shards[3].bits_written == {
circuit.bits[0],
circuit.bits[1],
circuit.bits[2],
circuit.bits[3],
}
assert shards[3].bits_read == {
circuit.bits[0],
circuit.bits[1],
circuit.bits[2],
circuit.bits[3],
}
assert shards[3].depends_upon == {shards[0].ID, shards[2].ID}
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class QasmFile(Enum):
tk2_same_angle = auto()
tk2_diff_angles = auto()
rxrz = auto()
classical_ordering = auto()


def get_qasm_as_circuit(qasm_file: QasmFile) -> "Circuit":
Expand Down

0 comments on commit 8e52dea

Please sign in to comment.