-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 bit signed arithmetic support (#57)
* feat: add support for negative integers to phir * feat: add hypothesis-enabled unit tests for BinArray2 Also, clamp only for unsigned integer types * lint: fix typos and exclude cuquantum_old * feat: replace BinArray with BinArray2 * fix: set BinArray2's value correctly, or it can convert to ndarray * build: bump patch version to distinguish in testing * feat: partition data_types into signed and unsigned also make size optional for signed integer type * build: update to correct phir version Co-authored-by: Ciarán Ryan-Anderson <[email protected]> --------- Co-authored-by: Ciarán Ryan-Anderson <[email protected]>
- Loading branch information
Showing
13 changed files
with
192 additions
and
29 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
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
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
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
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,35 @@ | ||
from pecos.engines.hybrid_engine import HybridEngine | ||
|
||
|
||
def bin2int(result: list[str]) -> int: | ||
return int(result[0], base=2) | ||
|
||
|
||
def test_setting_cvar(): | ||
phir = { | ||
"format": "PHIR/JSON", | ||
"version": "0.1.0", | ||
"ops": [ | ||
{"data": "cvar_define", "data_type": "i32", "variable": "var_i32"}, | ||
{"data": "cvar_define", "data_type": "u32", "variable": "var_u32", "size": 32}, | ||
{"data": "cvar_define", "data_type": "i64", "variable": "var_i64"}, | ||
{"data": "cvar_define", "data_type": "u64", "variable": "var_u64", "size": 64}, | ||
{"data": "cvar_define", "data_type": "i32", "variable": "var_i32neg"}, | ||
{"data": "cvar_define", "data_type": "i64", "variable": "var_i64neg"}, | ||
{"cop": "=", "returns": ["var_i32"], "args": [2**31 - 1]}, | ||
{"cop": "=", "returns": ["var_u32"], "args": [2**32 - 1]}, | ||
{"cop": "=", "returns": ["var_i64"], "args": [2**63 - 1]}, | ||
{"cop": "=", "returns": ["var_u64"], "args": [2**64 - 1]}, | ||
{"cop": "=", "returns": ["var_i32neg"], "args": [-(2**31)]}, | ||
{"cop": "=", "returns": ["var_i64neg"], "args": [-(2**63)]}, | ||
], | ||
} | ||
|
||
results = HybridEngine(qsim="stabilizer").run(program=phir, shots=5) | ||
|
||
assert bin2int(results["var_i32"]) == 2**31 - 1 | ||
assert bin2int(results["var_u32"]) == 2**32 - 1 | ||
assert bin2int(results["var_i64"]) == 2**63 - 1 | ||
assert bin2int(results["var_u64"]) == 2**64 - 1 | ||
assert bin2int(results["var_i32neg"]) == -(2**31) | ||
assert bin2int(results["var_i64neg"]) == -(2**63) |
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,104 @@ | ||
from typing import Final | ||
|
||
import numpy as np | ||
from hypothesis import assume, given | ||
from hypothesis import strategies as st | ||
from pecos.engines.cvm.binarray2 import BinArray2 as BinArray | ||
|
||
DEFAULT_SIZE: Final = 63 | ||
MIN: Final = -(2**DEFAULT_SIZE) | ||
MAX: Final = 2**DEFAULT_SIZE - 1 | ||
int_range = st.integers(min_value=MIN, max_value=MAX) | ||
|
||
|
||
@given(st.text(alphabet=["0", "1"], min_size=1)) | ||
def test_init(x): | ||
ba = BinArray(x) | ||
assert ba == f"0b{x}" | ||
|
||
|
||
def test_set_bit(): | ||
ba = BinArray("0000") | ||
ba[2] = 1 | ||
assert ba == 0b0100 | ||
|
||
|
||
def test_get_bit(): | ||
ba = BinArray("1010") | ||
assert ba[2] == 0 | ||
assert ba[3] == 1 | ||
|
||
|
||
def test_to_int(): | ||
ba = BinArray("1010") | ||
assert int(ba) == 10 | ||
|
||
|
||
@given(int_range, int_range) | ||
def test_addition(x, y): | ||
assume(MIN <= x + y <= MAX) | ||
ba1 = BinArray(DEFAULT_SIZE, x) | ||
ba2 = BinArray(DEFAULT_SIZE, y) | ||
result = ba1 + ba2 | ||
assert int(result) == x + y | ||
|
||
|
||
def test_subtraction(): | ||
ba1 = BinArray("1101") # 13 | ||
ba2 = BinArray("1010") # 10 | ||
result = ba1 - ba2 | ||
assert int(result) == 3 | ||
|
||
|
||
@given(int_range, int_range) | ||
def test_multiplication(x, y): | ||
assume(MIN <= x * y <= MAX) | ||
ba1 = BinArray(DEFAULT_SIZE, x) | ||
ba2 = BinArray(DEFAULT_SIZE, y) | ||
result = ba1 * ba2 | ||
assert int(result) == x * y | ||
|
||
|
||
def test_comparison(): | ||
ba1 = BinArray("1010") # 10 | ||
ba2 = BinArray("1010") # 10 | ||
ba3 = BinArray("1101") # 13 | ||
assert ba1 == ba2 | ||
assert ba1 != ba3 | ||
assert ba1 != ba3 | ||
assert ba1 < ba3 | ||
assert ba3 > ba1 | ||
|
||
|
||
def test_bitwise_and(): | ||
ba1 = BinArray("1010") # 10 | ||
ba2 = BinArray("1101") # 13 | ||
result = ba1 & ba2 | ||
assert result == 0b1000 | ||
|
||
|
||
def test_bitwise_or(): | ||
ba1 = BinArray("1010") # 10 | ||
ba2 = BinArray("1101") # 13 | ||
result = ba1 | ba2 | ||
assert result == 0b1111 | ||
|
||
|
||
def test_bitwise_xor(): | ||
ba1 = BinArray("1010") # 10 | ||
ba2 = BinArray("1101") # 13 | ||
result = ba1 ^ ba2 | ||
assert result == 0b0111 | ||
|
||
|
||
def test_unsigned_bitwise_not(): | ||
ba = BinArray("1010", dtype=np.uint64) # 10 | ||
result = ~ba | ||
assert result == 0b0101 | ||
|
||
|
||
@given(int_range) | ||
def test_signed_bitwise_not(x): | ||
ba = BinArray(DEFAULT_SIZE, x) | ||
result = ~ba | ||
assert int(result) == -x - 1 # (two's complement) |