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

Arbitrary qreg naming #120

Merged
merged 7 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions pytket/phir/sharding/shards2ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from typing import TYPE_CHECKING, TypeAlias

if TYPE_CHECKING:
from pytket.unit_id import UnitID

from .shard import Shard, ShardLayer

Layer: TypeAlias = list[list[int]]
Expand All @@ -22,6 +24,8 @@ def parse_shards_naive(
shards_in_layer: list[ShardLayer] = []
scheduled: set[int] = set()
num_shards: int = len(shards)
qid_count: int = 0
qubits2ids: dict["UnitID", int] = {}

while len(scheduled) < num_shards:
layer: Layer = []
Expand All @@ -39,17 +43,31 @@ def parse_shards_naive(
# if there are more than 2 qubits used, treat them all as parallel sq ops
# one qubit will just be a single sq op
# 3 or more will be 3 or more parallel sq ops
# when iterating through qubits,
# map all the qubits to a unique id to prevent duplicates in placement
if len(shard.qubits_used) != 2: # noqa: PLR2004
for qubit in shard.qubits_used:
op = qubit.index
qid, qid_count = get_qid(qubit, qubits2ids, qid_count)
op = [qid]
layer.append(op)
else:
for qubit in shard.qubits_used:
op.append(qubit.index[0])
qid, qid_count = get_qid(qubit, qubits2ids, qid_count)
op.append(qid)
layer.append(op)

scheduled.add(shard.ID)

layers.append(layer)

return layers, shards_in_layer


def get_qid(
qubit: "UnitID", qubits2ids: dict["UnitID", int], qid_count: int
) -> tuple[int, int]:
"""Get qubit ID even if it is missing in the dict."""
qid = qubits2ids.setdefault(qubit, qid_count)
if qid == qid_count:
qid_count += 1
return qid, qid_count
40 changes: 40 additions & 0 deletions tests/data/qasm/arbitrary_qreg_names.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
OPENQASM 2.0;
include "qelib1.inc";
include "hqslib1_dev.inc";

qreg q[2];
qreg a[2];
creg c[2];
creg l[2];
ry(0.5*pi) q[0];
rx(3.5*pi) q[1];
rz(3.5*pi) q[1];
ZZ q[0],q[1];
rz(3.5*pi) q[0];
ry(3.5*pi) q[1];
barrier q[0],q[1];
rz(3.5*pi) q[0];
rx(0.5*pi) q[1];
ry(0.5*pi) q[1];
ZZ q[0],q[1];
ry(3.5*pi) q[0];
rx(3.5*pi) q[1];
barrier q[0],q[1];
barrier q[0], a[0];
x a[0];
h a[0];
ZZ q[0], a[0];
barrier q[0], a[0];
ZZ q[0], a[0];
h a[0];
measure a[0] -> l[0];
measure q[0] -> c[0];
barrier q[1], a[1];
x a[1];
h a[1];
ZZ q[1], a[1];
barrier q[1], a[1];
ZZ q[1], a[1];
h a[1];
measure a[1] -> l[1];
measure q[1] -> c[1];
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class QasmFile(Enum):
classical_ordering = auto()
single_qubit_parallel_test = auto()
exec_order_two_qubits = auto()
arbitrary_qreg_names = auto()


class WatFile(Enum):
Expand Down