Skip to content

Commit

Permalink
Fixing support of PHIR's multi-assignment (#36)
Browse files Browse the repository at this point in the history
* Fixing support of PHIR's multi-assignment

* bump version to 0.5.0.dev6
  • Loading branch information
qciaran committed Jan 5, 2024
1 parent a40d0a7 commit 36b423f
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 7 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.dev5"
version = "0.5.0.dev6"
authors = [
{name = "The PECOS Developers"},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,11 +260,15 @@ def assign_int(self, cvar, val: int):
self.cenv[cid] = cval

def handle_cops(self, op):
"""Handle the processing of classical operations."""

if op.name == "=":
(arg,) = op.args
(rtn,) = op.returns
val = self.eval_expr(arg)
self.assign_int(rtn, val)
args = []
for a in op.args:
args.append(self.eval_expr(a))

for r, a in zip(op.returns, args):
self.assign_int(r, a)

elif isinstance(op, pt.opt.FFCall):
args = []
Expand Down
8 changes: 6 additions & 2 deletions python/pecos/reps/pypmir/pypmir.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,13 @@ def handle_op(cls, o: dict, p: PyPMIR) -> TypeOp:

@classmethod
def from_phir(cls, phir: dict) -> PyPMIR:
# TODO: Build nested instructions.
"""Converts PHIR dict to PyPMIR object."""

p = PyPMIR(metadata=dict(phir["metadata"]))
p = PyPMIR(
metadata=dict(
phir.get("metadata", {}),
),
)

next_qvar_int = 0

Expand Down
117 changes: 117 additions & 0 deletions tests/integration/test_phir_setting_cregs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
from pecos.engines.hybrid_engine import HybridEngine


def test_setting_bits1():
phir = {
"format": "PHIR/JSON",
"version": "0.1.0",
"ops": [
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 3},
# c[0], c[1], c[2] = True, False, True
{"cop": "=", "returns": [["c", 0], ["c", 1], ["c", 2]], "args": [True, False, True]},
],
}

results = HybridEngine(qsim="stabilizer").run(program=phir, shots=5)

assert results["c"].count("101") == len(results["c"])


def test_setting_bits2():
phir = {
"format": "PHIR/JSON",
"version": "0.1.0",
"ops": [
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 3},
# c[0], c[1], c[2] = 0, 1, 1
{"cop": "=", "returns": [["c", 0], ["c", 1], ["c", 2]], "args": [0, 1, 1]},
],
}

results = HybridEngine(qsim="stabilizer").run(program=phir, shots=5)

assert results["c"].count("110") == len(results["c"])


def test_setting_cvar():
phir = {
"format": "PHIR/JSON",
"version": "0.1.0",
"ops": [
{"data": "cvar_define", "data_type": "u32", "variable": "a", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "b", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 3},
# a, b, c = 0, 1, 2
{"cop": "=", "returns": ["a", "b", "c"], "args": [0, 1, 2]},
],
}

results = HybridEngine(qsim="stabilizer").run(program=phir, shots=5)

assert results["a"].count("000") == len(results["a"])
assert results["b"].count("001") == len(results["b"])
assert results["c"].count("010") == len(results["c"])


def test_setting_expr():
phir = {
"format": "PHIR/JSON",
"version": "0.1.0",
"ops": [
{"data": "cvar_define", "data_type": "u32", "variable": "a", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "b", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 3},
# a, b, c = 0+1, a+1, c[1]+2
{
"cop": "=",
"returns": ["a", "b", "c"],
"args": [
{"cop": "+", "args": [0, 1]},
{"cop": "+", "args": ["a", 1]},
{"cop": "+", "args": [["c", 1], 2]},
],
},
],
}

results = HybridEngine(qsim="stabilizer").run(program=phir, shots=5)

assert results["a"].count("001") == len(results["a"])
assert results["b"].count("001") == len(results["b"])
assert results["c"].count("010") == len(results["c"])


def test_setting_mixed():
phir = {
"format": "PHIR/JSON",
"version": "0.1.0",
"ops": [
{"data": "cvar_define", "data_type": "u32", "variable": "a", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "b", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "c", "size": 3},
{"data": "cvar_define", "data_type": "u32", "variable": "d", "size": 3},
# a[0], b, c, d[2] = 1, 2, c[1]+2, a[0] + 1
{
"cop": "=",
"returns": [
["a", 0],
"b",
"c",
["d", 2],
],
"args": [
1,
3,
{"cop": "+", "args": [["c", 1], 2]},
{"cop": "+", "args": [["a", 0], 1]},
],
},
],
}

results = HybridEngine(qsim="stabilizer").run(program=phir, shots=5)

assert results["a"].count("001") == len(results["a"])
assert results["b"].count("011") == len(results["b"])
assert results["c"].count("010") == len(results["c"])
assert results["d"].count("100") == len(results["d"])

0 comments on commit 36b423f

Please sign in to comment.