Skip to content

Commit

Permalink
Merge pull request #144 from CQCL/duration-must-be-float
Browse files Browse the repository at this point in the history
  • Loading branch information
qartik committed Mar 13, 2024
2 parents ee9837a + d36c748 commit c940257
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ repos:
- id: ruff-format

- repo: https://github.com/dosisod/refurb
rev: v1.28.0
rev: v2.0.0
hooks:
- id: refurb

Expand Down
2 changes: 1 addition & 1 deletion pytket/phir/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def main() -> None:
parser.add_argument(
"--version",
action="version",
version=str(version("pytket-phir")),
version=version("pytket-phir"),
)
args = parser.parse_args()

Expand Down
6 changes: 4 additions & 2 deletions pytket/phir/phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None:
out = {
"mop": "Idle",
"args": [arg_to_bit(qbit) for qbit in cmd.qubits],
"duration": (dur, "s"),
"duration": (float(dur), "s"),
}
case "order" | "group":
raise NotImplementedError(op.data)
Expand Down Expand Up @@ -299,7 +299,9 @@ def convert_subcmd(op: tk.Op, cmd: tk.Command) -> JsonDict | None:
if len(cmd.bits) != len(op.values):
logger.error("LHS and RHS lengths mismatch for classical assignment")
raise ValueError
out = assign_cop([arg_to_bit(bit) for bit in cmd.bits], op.values)
out = assign_cop(
[arg_to_bit(bit) for bit in cmd.bits], list(map(int, op.values))
)

case tk.CopyBitsOp():
if len(cmd.bits) != len(cmd.args) // 2:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ ruff==0.3.2
setuptools_scm==8.0.4
sphinx==7.2.6
wasmtime==18.0.2
wheel==0.42.0
wheel==0.43.0
6 changes: 3 additions & 3 deletions tests/test_phirgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_sleep_idle() -> None:
"""Ensure sleep from qasm gets converted to PHIR Idle Mop."""
circ = get_qasm_as_circuit(QasmFile.sleep)
phir = json.loads(pytket_to_phir(circ))
assert phir["ops"][7] == {"mop": "Idle", "args": [["q", 0]], "duration": ["1", "s"]}
assert phir["ops"][7] == {"mop": "Idle", "args": [["q", 0]], "duration": [1.0, "s"]}


def test_multiple_sleep() -> None:
Expand All @@ -148,5 +148,5 @@ def test_multiple_sleep() -> None:
"""
circ = circuit_from_qasm_str(qasm)
phir = json.loads(pytket_to_phir(circ))
assert phir["ops"][2] == {"mop": "Idle", "args": [["q", 0]], "duration": ["1", "s"]}
assert phir["ops"][4] == {"mop": "Idle", "args": [["q", 1]], "duration": ["2", "s"]}
assert phir["ops"][2] == {"mop": "Idle", "args": [["q", 0]], "duration": [1.0, "s"]}
assert phir["ops"][4] == {"mop": "Idle", "args": [["q", 1]], "duration": [2.0, "s"]}
70 changes: 35 additions & 35 deletions tests/test_sharder.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ def test_with_baby_circuit(self) -> None:
assert len(shards[0].sub_commands) == 2
sub_commands = list(shards[0].sub_commands.items())
assert sub_commands[0][1][0].op.type == OpType.H
assert len(shards[0].depends_upon) == 0
assert not shards[0].depends_upon

assert shards[1].primary_command.op.type == OpType.Measure
assert len(shards[1].sub_commands) == 0
assert not shards[1].sub_commands
assert shards[1].depends_upon == {shards[0].ID}

assert shards[2].primary_command.op.type == OpType.Measure
assert len(shards[2].sub_commands) == 0
assert not shards[2].sub_commands
assert shards[2].depends_upon == {shards[0].ID}

def test_rollup_behavior(self) -> None:
Expand All @@ -79,16 +79,16 @@ def test_rollup_behavior(self) -> None:
assert len(shards[0].sub_commands) == 2
sub_commands = list(shards[0].sub_commands.items())
assert sub_commands[0][1][0].op.type == OpType.H
assert len(shards[0].depends_upon) == 0
assert not shards[0].depends_upon

# Shard 1: Measure q[0] -> c[0] (TKET splits q->c)
assert shards[1].primary_command.op.type == OpType.Measure
assert len(shards[1].sub_commands) == 0
assert not shards[1].sub_commands
assert shards[1].depends_upon == {shards[0].ID}

# shard 2: Measure q[1] -> c[1]
assert shards[2].primary_command.op.type == OpType.Measure
assert len(shards[2].sub_commands) == 0
assert not shards[2].sub_commands
assert shards[2].depends_upon == {shards[0].ID}

# Shard 3: Rollup barrier for a lingering hadamard on q[0]
Expand All @@ -111,7 +111,7 @@ def test_simple_conditional(self) -> None:
assert shards[0].primary_command.op.type == OpType.Measure
assert shards[0].qubits_used == {circuit.qubits[0]}
assert shards[0].bits_written == {circuit.bits[0]}
assert shards[0].depends_upon == set()
assert not shards[0].depends_upon
assert len(shards[0].sub_commands.items()) == 1
s0_qubit, s0_sub_cmds = next(iter(shards[0].sub_commands.items()))
assert s0_qubit == circuit.qubits[0]
Expand All @@ -122,14 +122,14 @@ def test_simple_conditional(self) -> None:
assert len(shards[1].sub_commands.items()) == 0
assert shards[1].qubits_used == {circuit.qubits[0]}
assert shards[1].depends_upon == {shards[0].ID}
assert shards[1].bits_written == set()
assert shards[1].bits_read == set()
assert not shards[1].bits_written
assert not shards[1].bits_read

# shard 2: if (c==1) z=1;
assert shards[2].primary_command.op.type == OpType.Conditional
assert cast(Conditional, shards[2].primary_command.op).op.type == OpType.SetBits
assert len(shards[2].sub_commands.keys()) == 0
assert shards[2].qubits_used == set()
assert not shards[2].sub_commands
assert not shards[2].qubits_used
assert shards[2].bits_written == {circuit.bits[1]}
assert shards[2].bits_read == {circuit.bits[0], circuit.bits[1]}
assert shards[2].depends_upon == {shards[0].ID}
Expand All @@ -156,10 +156,10 @@ def test_complex_barriers(self) -> None: # noqa: PLR0915
# shard 0: [], c[3] = 1
assert shards[0].primary_command.op.type == OpType.SetBits
assert len(shards[0].sub_commands.items()) == 0
assert shards[0].qubits_used == set()
assert not shards[0].qubits_used
assert shards[0].bits_written == {circuit.bits[3]}
assert shards[0].bits_read == {circuit.bits[3]} # bits written are always read
assert shards[0].depends_upon == set()
assert not shards[0].depends_upon

# shard 1: [h q[0]; h q[1];] barrier q[0], q[1], c[3];
assert shards[1].primary_command.op.type == OpType.Barrier
Expand All @@ -181,8 +181,8 @@ def test_complex_barriers(self) -> None: # noqa: PLR0915
assert shards[2].primary_command.op.type == OpType.CX
assert len(shards[2].sub_commands.items()) == 0
assert shards[2].qubits_used == {circuit.qubits[0], circuit.qubits[1]}
assert shards[2].bits_written == set()
assert shards[2].bits_read == set()
assert not shards[2].bits_written
assert not shards[2].bits_read
assert shards[2].depends_upon == {shards[1].ID}

# shard 3: measure q[0]->c[0];
Expand Down Expand Up @@ -211,16 +211,16 @@ def test_complex_barriers(self) -> None: # noqa: PLR0915
assert shard_4_q3_cmds[2].op.type == OpType.X
assert shard_4_q3_cmds[2].qubits == [circuit.qubits[3]]
assert shards[4].qubits_used == {circuit.qubits[2], circuit.qubits[3]}
assert shards[4].bits_written == set()
assert shards[4].bits_read == set()
assert shards[4].depends_upon == set()
assert not shards[4].bits_written
assert not shards[4].bits_read
assert not shards[4].depends_upon

# shard 5: [] CX q[2], q[3];
assert shards[5].primary_command.op.type == OpType.CX
assert len(shards[5].sub_commands.items()) == 0
assert shards[5].qubits_used == {circuit.qubits[2], circuit.qubits[3]}
assert shards[5].bits_written == set()
assert shards[5].bits_read == set()
assert not shards[5].bits_written
assert not shards[5].bits_read
assert shards[5].depends_upon == {shards[4].ID}

# shard 6: measure q[2]->c[2];
Expand All @@ -243,7 +243,7 @@ def test_classical_hazards(self) -> None:
assert shards[0].qubits_used == {circuit.qubits[0]}
assert shards[0].bits_written == {circuit.bits[0]}
assert shards[0].bits_read == {circuit.bits[0]}
assert shards[0].depends_upon == set()
assert not shards[0].depends_upon

# shard 1: [H q[1];] measure q[1]->c[2];
# NOTE: pytket reorganizes circuits to be efficiently ordered
Expand All @@ -252,28 +252,28 @@ def test_classical_hazards(self) -> None:
assert shards[1].qubits_used == {circuit.qubits[1]}
assert shards[1].bits_written == {circuit.bits[2]}
assert shards[1].bits_read == {circuit.bits[2]}
assert shards[1].depends_upon == set()
assert not shards[1].depends_upon

# shard 2: [] if(c[0]==1) c[1]=1;
assert shards[2].primary_command.op.type == OpType.Conditional
assert len(shards[2].sub_commands) == 0
assert shards[2].qubits_used == set()
assert not shards[2].sub_commands
assert not shards[2].qubits_used
assert shards[2].bits_written == {circuit.bits[1]}
assert shards[2].bits_read == {circuit.bits[1], circuit.bits[0]}
assert shards[2].depends_upon == {shards[0].ID}

# shard 3: [] c[0]=0;
assert shards[3].primary_command.op.type == OpType.SetBits
assert len(shards[2].sub_commands) == 0
assert shards[3].qubits_used == set()
assert not shards[2].sub_commands
assert not shards[3].qubits_used
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[2].ID}

# shard 4: [] if(c[2]==1) c[0]=1;
assert shards[4].primary_command.op.type == OpType.Conditional
assert len(shards[4].sub_commands) == 0
assert shards[4].qubits_used == set()
assert not shards[4].sub_commands
assert not shards[4].qubits_used
assert shards[4].bits_written == {circuit.bits[0]}
assert shards[4].bits_read == {circuit.bits[0], circuit.bits[2]}
assert shards[4].depends_upon == {shards[1].ID, shards[3].ID}
Expand All @@ -293,11 +293,11 @@ def test_with_big_gate(self) -> None:
circuit.qubits[2],
circuit.qubits[3],
}
assert shards[0].bits_written == set()
assert not shards[0].bits_written

# shard 1: [] measure q[3]->[c0]
assert shards[1].primary_command.op.type == OpType.Measure
assert len(shards[1].sub_commands) == 0
assert not shards[1].sub_commands
assert shards[1].qubits_used == {circuit.qubits[3]}
assert shards[1].bits_written == {circuit.bits[0]}

Expand All @@ -310,7 +310,7 @@ def test_classical_ordering_breaking_circuit(self) -> None:
# 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 not shards[0].qubits_used
assert shards[0].bits_written == {
circuit.bits[0],
circuit.bits[1],
Expand All @@ -323,12 +323,12 @@ def test_classical_ordering_breaking_circuit(self) -> None:
circuit.bits[2],
circuit.bits[3],
}
assert shards[0].depends_upon == set()
assert not shards[0].depends_upon

# 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 not shards[1].qubits_used
assert shards[1].bits_written == {
circuit.bits[4],
circuit.bits[5],
Expand All @@ -350,7 +350,7 @@ def test_classical_ordering_breaking_circuit(self) -> None:
# 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 not shards[2].qubits_used
assert shards[2].bits_written == {
circuit.bits[8],
circuit.bits[9],
Expand All @@ -376,7 +376,7 @@ def test_classical_ordering_breaking_circuit(self) -> None:
# 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 not shards[3].qubits_used
assert shards[3].bits_written == {
circuit.bits[0],
circuit.bits[1],
Expand Down

0 comments on commit c940257

Please sign in to comment.