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

Allow constant BitLogicExp as condition #1397

Merged
merged 3 commits into from
May 15, 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
2 changes: 2 additions & 0 deletions pytket/docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Features:
* Add two new status values for circuits on backends: "CANCELLING" and "RETRYING".
* Use `lark` package instead of deprecated `lark-parser`.
* Add ``GreedyPauliSimp`` optimisation pass.
* Add ``BitWiseOp.ZERO`` and ``BitWiseOp.ONE`` to allow construction of constant
conditional expressions.

Fixes:

Expand Down
39 changes: 39 additions & 0 deletions pytket/pytket/circuit/logic_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class BitWiseOp(Enum):
EQ = "=="
NEQ = "!="
NOT = "~"
ZERO = "0"
ONE = "1"


class RegWiseOp(Enum):
Expand Down Expand Up @@ -110,6 +112,10 @@ def factory(cls, op: Ops) -> Type["LogicExp"]:
return BitEq
if op == BitWiseOp.NEQ:
return BitNeq
if op == BitWiseOp.ZERO:
return BitZero
if op == BitWiseOp.ONE:
return BitOne
if op == RegWiseOp.AND:
return RegAnd
if op == RegWiseOp.OR:
Expand Down Expand Up @@ -368,6 +374,13 @@ def __str__(self) -> str:
return f"({self.op.value} {self.args[0]})"


class NullaryOp(LogicExp):
"""Expression for operation on no arguments (i.e. constant)."""

def __str__(self) -> str:
return f"({self.op.value})"


class And(BinaryOp):
@staticmethod
def _const_eval(args: List[Constant]) -> Constant:
Expand Down Expand Up @@ -426,6 +439,26 @@ def _const_eval(args: List[Constant]) -> Constant:
return 1 - args[0]


class BitZero(NullaryOp, BitLogicExp):
def __init__(self) -> None:
self.op = BitWiseOp.ZERO
self.args = []

@staticmethod
def _const_eval(args: List[Constant]) -> Constant:
return 0


class BitOne(NullaryOp, BitLogicExp):
def __init__(self) -> None:
self.op = BitWiseOp.ONE
self.args = []

@staticmethod
def _const_eval(args: List[Constant]) -> Constant:
return 1


class RegAnd(And, RegLogicExp):
def __init__(self, arg1: RegArgType, arg2: RegArgType) -> None:
self.op = RegWiseOp.AND
Expand Down Expand Up @@ -647,6 +680,12 @@ def create_bit_logic_exp(op: BitWiseOp, args: Sequence[BitArgType]) -> BitLogicE
if op == BitWiseOp.NEQ:
assert len(args) == 2
return BitNeq(args[0], args[1])
if op == BitWiseOp.ZERO:
assert len(args) == 0
return BitZero()
if op == BitWiseOp.ONE:
assert len(args) == 0
return BitOne()


def create_reg_logic_exp(op: RegWiseOp, args: Sequence[RegArgType]) -> RegLogicExp:
Expand Down
19 changes: 12 additions & 7 deletions pytket/tests/classical_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
RegPow,
RegWiseOp,
UnaryOp,
NullaryOp,
reg_eq,
reg_geq,
reg_gt,
Expand Down Expand Up @@ -763,14 +764,16 @@ def primitive_bit_logic_exps(
bits: SearchStrategy[Bit] = bits(),
) -> BitLogicExp:
op = draw(ops)
args = [draw(bits)]
args = []
exp_type = LogicExp.factory(op)
if issubclass(exp_type, BinaryOp):
if issubclass(exp_type, PredicateExp):
const_compare = draw(binary_digits)
args.append(Bit(const_compare))
else:
args.append(draw(bits))
if not issubclass(exp_type, NullaryOp):
args.append(draw(bits))
if issubclass(exp_type, BinaryOp):
if issubclass(exp_type, PredicateExp):
const_compare = draw(binary_digits)
args.append(Bit(const_compare))
else:
args.append(draw(bits))
exp = create_bit_logic_exp(op, args)
assert isinstance(exp, BitLogicExp)
return exp
Expand Down Expand Up @@ -798,6 +801,8 @@ def test_bit_exp(bit_exp: BitLogicExp, constants: Tuple[int, int]) -> None:
BitWiseOp.NOT: operator.not_,
BitWiseOp.EQ: operator.eq,
BitWiseOp.NEQ: operator.ne,
BitWiseOp.ZERO: lambda: 0,
BitWiseOp.ONE: lambda: 1,
}
op_map = {key: overflow_wrapper(val, 2) for key, val in op_map.items()}
eval_val = bit_exp.eval_vals()
Expand Down
23 changes: 23 additions & 0 deletions pytket/tests/qasm_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
CustomGate,
)
from pytket.circuit.decompose_classical import DecomposeClassicalError
from pytket.circuit.logic_exp import BitWiseOp, create_bit_logic_exp
from pytket.qasm import (
circuit_from_qasm,
circuit_to_qasm,
Expand Down Expand Up @@ -932,6 +933,28 @@ def test_conditional_nonstandard_gates() -> None:
assert "if(c==1) zzmax q[0],q[1];" in qasm


def test_const_condition() -> None:
# https://github.com/CQCL/tket/issues/1383
circ = Circuit(1, 1)
exp = create_bit_logic_exp(BitWiseOp.ONE, [])
circ.H(0, condition=exp)
circ.measure_all()
qasm = circuit_to_qasm_str(circ, header="hqslib1")
assert (
qasm
== """OPENQASM 2.0;
include "hqslib1.inc";

qreg q[1];
creg c[1];
creg tk_SCRATCH_BIT[1];
tk_SCRATCH_BIT[0] = (1);
if(tk_SCRATCH_BIT[0]==1) h q[0];
measure q[0] -> c[0];
"""
)


if __name__ == "__main__":
test_qasm_correct()
test_qasm_qubit()
Expand Down
Loading