generated from CQCL/pytemplate
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving shard handling and testing (#3)
* Improving shard handling and testing * Apply suggestions from code review Co-authored-by: Kartik Singhal <[email protected]> * in progress * test progress * fixing ruff * removing commented * linting feedback * mypy * Adding test for partial barriers * updating test * cleanup comments * another classical test * big gate test * hash method for shards to allow sets --------- Co-authored-by: Neal Erickson <[email protected]> Co-authored-by: Kartik Singhal <[email protected]>
- Loading branch information
1 parent
a77f979
commit 90565f1
Showing
12 changed files
with
557 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,59 @@ | ||
from dataclasses import dataclass | ||
import io | ||
from dataclasses import dataclass, field | ||
from itertools import count | ||
|
||
from pytket.circuit import Command | ||
from pytket.unit_id import UnitID | ||
from pytket.unit_id import Bit, Qubit, UnitID | ||
|
||
|
||
@dataclass | ||
@dataclass(frozen=True) | ||
class Shard: | ||
"""The Shard class. | ||
A shard is a logical grouping of operations that represents the unit by which | ||
we actually do placement of qubits. | ||
""" | ||
|
||
# The schedulable command of the shard | ||
# The unique identifier of the shard | ||
ID: int = field(default_factory=count().__next__, init=False) | ||
|
||
# The "schedulable" command of the shard | ||
primary_command: Command | ||
|
||
# The other commands related to the primary schedulable command, stored | ||
# as a map of bit-handle (unitID) -> list[Command] | ||
sub_commands: dict[UnitID, list[Command]] | ||
|
||
# A set of the other shards this particular shard depends upon, and thus | ||
# must be scheduled after | ||
depends_upon: set["Shard"] | ||
# All qubits used by the primary and sub commands | ||
qubits_used: set[Qubit] | ||
|
||
# Set of all classical bits written to by the primary and sub commands | ||
bits_written: set[Bit] | ||
|
||
# Set of all classical bits read by the primary and sub commands | ||
bits_read: set[Bit] | ||
|
||
# A set of the identifiers of other shards this particular shard depends upon | ||
depends_upon: set[int] | ||
|
||
def __hash__(self) -> int: | ||
"""Hashing for shards is done only by its autogen unique int ID.""" | ||
return self.ID | ||
|
||
def pretty_print(self) -> str: | ||
"""Returns the shard in a human-friendly format.""" | ||
output = io.StringIO() | ||
output.write(f"Shard {self.ID}:") | ||
output.write(f"\n Command: {self.primary_command}") | ||
output.write("\n Sub commands: ") | ||
if not self.sub_commands: | ||
output.write("[]") | ||
for sub in self.sub_commands: | ||
output.write(f"\n {sub}: {self.sub_commands[sub]}") | ||
output.write(f"\n Qubits used: {self.qubits_used}") | ||
output.write(f"\n Bits written: {self.bits_written}") | ||
output.write(f"\n Bits read: {self.bits_read}") | ||
output.write(f"\n Depends upon: {self.depends_upon}") | ||
content = output.getvalue() | ||
output.close() | ||
return content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
OPENQASM 2.0; | ||
include "hqslib1.inc"; | ||
|
||
qreg q[2]; | ||
creg c[2]; | ||
|
||
h q[0]; | ||
h q[1]; | ||
CX q[0], q[1]; | ||
|
||
measure q->c; | ||
|
||
h q[0]; | ||
|
||
h q[1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
OPENQASM 2.0; | ||
include "hqslib1.inc"; | ||
|
||
qreg q[4]; | ||
creg c[4]; | ||
|
||
h q[0]; | ||
h q[1]; | ||
h q[2]; | ||
h q[3]; | ||
c[3] = 1; | ||
|
||
barrier q[0], q[1], c[3]; | ||
|
||
CX q[0], q[1]; | ||
|
||
measure q[0]->c[0]; | ||
|
||
h q[2]; | ||
h q[3]; | ||
x q[3]; | ||
|
||
barrier q[2], q[3]; | ||
|
||
CX q[2], q[3]; | ||
|
||
measure q[2]->c[2]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
OPENQASM 2.0; | ||
include "hqslib1.inc"; | ||
|
||
creg c[1]; | ||
qreg q[4]; | ||
|
||
h q[0]; | ||
h q[1]; | ||
h q[2]; | ||
|
||
c4x q[0],q[1],q[2],q[3]; | ||
|
||
measure q[3] -> c[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
OPENQASM 2.0; | ||
include "hqslib1.inc"; | ||
|
||
qreg q[2]; | ||
creg c[3]; | ||
|
||
h q[0]; | ||
|
||
measure q[0]->c[0]; | ||
|
||
if(c[0]==1) c[1]=1; // RAW | ||
|
||
c[0]=0; // WAR | ||
|
||
h q[1]; | ||
|
||
measure q[1]->c[2]; | ||
|
||
if(c[2]==1) c[0]=1; // WAW |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
OPENQASM 2.0; | ||
include "hqslib1_dev.inc"; | ||
qreg q[1]; | ||
creg a[10]; | ||
creg b[10]; | ||
creg c[4]; | ||
|
||
// classical assignment of registers | ||
a[0] = 1; | ||
a = 3; | ||
// classical bitwise functions | ||
a = 1; | ||
b = 3; | ||
c = a ^ b; // XOR | ||
// evaluating a beyond creg == int | ||
a = 1; | ||
b = 2; | ||
if(a[0]==1) x q[0]; | ||
if(a!=1) x q[0]; | ||
if(a>1) x q[0]; | ||
if(a<1) x q[0]; | ||
if(a>=1) x q[0]; | ||
if(a<=1) x q[0]; | ||
if (a==10) b=1; | ||
measure q[0] -> c[0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
OPENQASM 2.0; | ||
include "hqslib1.inc"; | ||
|
||
qreg q[1]; | ||
creg c[1]; | ||
creg z[1]; | ||
|
||
h q; | ||
measure q->c; | ||
reset q; | ||
if (c==1) h q; | ||
if (c==1) z=1; | ||
measure q->c; |
Oops, something went wrong.