From 971ce40358d9c1581063f89af6b004d0b948b78c Mon Sep 17 00:00:00 2001 From: Cody Wang Date: Wed, 1 May 2024 15:22:58 -0700 Subject: [PATCH] fix: Integer division for `IntegerLiteral`s (#247) --- .../openqasm/_helpers/functions.py | 2 +- .../openqasm/test_interpreter.py | 27 ++++++++++--------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/braket/default_simulator/openqasm/_helpers/functions.py b/src/braket/default_simulator/openqasm/_helpers/functions.py index 7e7d45d0..0fe9f20c 100644 --- a/src/braket/default_simulator/openqasm/_helpers/functions.py +++ b/src/braket/default_simulator/openqasm/_helpers/functions.py @@ -33,7 +33,7 @@ getattr(BinaryOperator, "+"): lambda x, y: IntegerLiteral(x.value + y.value), getattr(BinaryOperator, "-"): lambda x, y: IntegerLiteral(x.value - y.value), getattr(BinaryOperator, "*"): lambda x, y: IntegerLiteral(x.value * y.value), - getattr(BinaryOperator, "/"): lambda x, y: IntegerLiteral(x.value / y.value), + getattr(BinaryOperator, "/"): lambda x, y: IntegerLiteral(x.value // y.value), getattr(BinaryOperator, "%"): lambda x, y: IntegerLiteral(x.value % y.value), getattr(BinaryOperator, "**"): lambda x, y: IntegerLiteral(x.value**y.value), getattr(UnaryOperator, "-"): lambda x: IntegerLiteral(-x.value), diff --git a/test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py b/test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py index 73333b9a..9f468e15 100644 --- a/test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py +++ b/test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py @@ -1017,7 +1017,7 @@ def test_pow(): pow(two) @ cx c, a; } gate cxx_2 c, a { - pow(1/2) @ pow(4) @ cx c, a; + pow(1./2.) @ pow(4) @ cx c, a; } gate cxxx c, a { pow(1) @ pow(two) @ cx c, a; @@ -1029,18 +1029,19 @@ def test_pow(): qubit q4; qubit q5; - pow(1/2) @ x q1; // half flip - pow(1/2) @ x q1; // half flip - cx q1, q2; // flip - cxx_1 q1, q3; // don't flip - cxx_2 q1, q4; // don't flip - cnot q1, q5; // flip - x q3; // flip - x q4; // flip - - s q1; // sqrt z - s q1; // again - inv @ z q1; // inv z + pow(1./2) @ x q1; // half flip + pow(1/2.) @ x q1; // half flip + cx q1, q2; // flip + cxx_1 q1, q3; // don't flip + cxx_2 q1, q4; // don't flip + cnot q1, q5; // flip + x q3; // flip + x q4; // flip + pow(1/2) @ x q5; // don't flip + + s q1; // sqrt z + s q1; // again + inv @ z q1; // inv z """ circuit = Interpreter().build_circuit(qasm) simulation = StateVectorSimulation(5, 1, 1)