Skip to content

Commit

Permalink
Bug phir if statement (#27)
Browse files Browse the repository at this point in the history
* Fixed issue with yielding instructions of blocks

* bump to 0.5.0.dev3 + avoiding [] buffer yielding

* Fixing linting because my pre-commit hook was off
  • Loading branch information
qciaran authored Dec 7, 2023
1 parent bf396bb commit 703eccb
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "quantum-pecos"
version = "0.5.0.dev2"
version = "0.5.0.dev3"
authors = [
{name = "The PECOS Developers"},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def execute(self, sequence: Sequence) -> Generator[list, Any, None]:
elif isinstance(op, pt.opt.COp):
self.handle_cops(op)

elif isinstance(op, pt.block.IfBlock):
elif isinstance(op, pt.block.Block):
yield from self.execute_block(op)

elif isinstance(op, pt.opt.MOp):
Expand All @@ -146,6 +146,9 @@ def execute(self, sequence: Sequence) -> Generator[list, Any, None]:
msg = f"Statement not recognized: {op}"
raise TypeError(msg)

if op_buffer:
yield op_buffer

def get_cval(self, cvar):
cid = self.program.csym2id[cvar]
return self.cenv[cid]
Expand All @@ -155,7 +158,7 @@ def get_bit(self, cvar, idx):
val >>= idx
return val

def eval_expr(self, expr: int | (str | (list | dict))) -> int:
def eval_expr(self, expr: int | (str | (list | dict))) -> int | None:
if isinstance(expr, int):
return expr
elif isinstance(expr, str):
Expand Down
6 changes: 6 additions & 0 deletions python/pecos/reps/pypmir/op_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ def __init__(
)
self.angles = angles

def __repr__(self):
return (
f"<QOP: {self.name} angles: {self.angles} args: {self.args} returns: {self.returns} "
f"meta: {self.metadata}>"
)


class COp(Op):
"""Classical operation."""
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/phir/classical_00_11.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"format": "PHIR/JSON",
"version": "0.1.0",
"metadata": {},
"ops": [
{"data": "qvar_define", "data_type": "qubits", "variable": "q", "size": 2},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 2},
{"qop": "H", "angles": null, "args": [["q", 0]]},
{"qop": "Measure", "returns": [["c", 0]], "args": [["q", 0]]},
{
"block": "if",
"condition": {"cop": "==", "args": [["c", 0], 1]},
"true_branch": [{"qop": "X", "angles": null, "args": [["q", 1]]}]
},
{"qop": "Measure", "returns": [["c", 1]], "args": [["q", 1]]}
]
}
11 changes: 11 additions & 0 deletions tests/integration/phir/recording_random_meas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"format": "PHIR/JSON",
"version": "0.1.0",
"metadata": {},
"ops": [
{"data": "qvar_define", "data_type": "qubits", "variable": "q", "size": 2},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 2},
{"qop": "H", "angles": null, "args": [["q", 0]]},
{"qop": "Measure", "returns": [["c", 0]], "args": [["q", 0]]}
]
}
25 changes: 25 additions & 0 deletions tests/integration/test_phir.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,28 @@ def test_example1_no_wasm_noisy():
program=example1_no_wasm_phir,
shots=100,
)


def test_record_random_bit():
"""Applying H and recording both 0 and 1."""

results = HybridEngine(qsim="stabilizer").run(
program=json.load(Path.open(this_dir / "phir" / "recording_random_meas.json")),
shots=100,
)

print(results)
c = results["c"]
assert c.count("01") + c.count("00") == len(c)


def test_classical_if_00_11():
"""Testing using an H + measurement and a conditional X gate to get 00 or 11."""

results = HybridEngine(qsim="stabilizer").run(
program=json.load(Path.open(this_dir / "phir" / "classical_00_11.json")),
shots=100,
)

c = results["c"]
assert c.count("00") + c.count("11") == len(c)

0 comments on commit 703eccb

Please sign in to comment.