Skip to content

Commit ffd586b

Browse files
committed
[amazon-braket#240] AttributeError: 'ArrayLiteral' object has no attribute 'value'.
Fixes bug [amazon-braket#240] by casting node.size into IntegerLiteral
1 parent a4d7f98 commit ffd586b

File tree

3 files changed

+90
-6
lines changed

3 files changed

+90
-6
lines changed

src/braket/default_simulator/openqasm/_helpers/functions.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,14 @@
9898
[BooleanLiteral(xv.value ^ yv.value) for xv, yv in zip(x.values, y.values)]
9999
),
100100
getattr(BinaryOperator, "<<"): lambda x, y: ArrayLiteral(
101-
x.values[y.value :] + [BooleanLiteral(False) for _ in range(y.value)]
101+
x.values[len(y.values) :] + [BooleanLiteral(False) for _ in range(len(y.values))]
102+
if isinstance(y, ArrayLiteral)
103+
else x.values[y.value :] + [BooleanLiteral(False) for _ in range(y.value)]
102104
),
103105
getattr(BinaryOperator, ">>"): lambda x, y: ArrayLiteral(
104-
[BooleanLiteral(False) for _ in range(y.value)] + x.values[: len(x.values) - y.value]
106+
[BooleanLiteral(False) for _ in range(len(y.values))] + x.values[: len(x.values) - len(y.values)]
107+
if isinstance(y, ArrayLiteral)
108+
else [BooleanLiteral(False) for _ in range(y.value)] + x.values[: len(x.values) - y.value]
105109
),
106110
getattr(UnaryOperator, "~"): lambda x: ArrayLiteral(
107111
[BooleanLiteral(not v.value) for v in x.values]

src/braket/default_simulator/openqasm/interpreter.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,13 @@ def _(self, node: Identifier) -> LiteralType:
247247

248248
@visit.register
249249
def _(self, node: QubitDeclaration) -> None:
250-
size = self.visit(node.size).value if node.size else 1
251-
self.context.add_qubits(node.qubit.name, size)
250+
size_arg = self.visit(node.size)
251+
if isinstance(size_arg, ArrayLiteral) and size_arg:
252+
size = ''.join(str(cast_to(IntegerLiteral, qubit).value) for qubit in size_arg.values)
253+
self.context.add_qubits(node.qubit.name, int(size, 2))
254+
else:
255+
size = size_arg.value if size_arg else 1
256+
self.context.add_qubits(node.qubit.name, size)
252257

253258
@visit.register
254259
def _(self, node: QuantumReset) -> None:

test/unit_tests/braket/default_simulator/openqasm/test_interpreter.py

+77-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2-
#
1+
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved #
32
# Licensed under the Apache License, Version 2.0 (the "License"). You
43
# may not use this file except in compliance with the License. A copy of
54
# the License is located at
@@ -2207,3 +2206,79 @@ def test_measure_invalid_qubit():
22072206
def test_measure_qubit_out_of_range(qasm, expected):
22082207
with pytest.raises(IndexError, match=expected):
22092208
Interpreter().build_circuit(qasm)
2209+
2210+
2211+
@pytest.mark.parametrize(
2212+
"qasm, expected",[
2213+
(
2214+
'''
2215+
bit[2] b;
2216+
qubit["10"] r1;
2217+
b = measure r1;
2218+
''',
2219+
[0, 1]
2220+
),
2221+
(
2222+
'''
2223+
bit[3] b;
2224+
qubit["11"] r1;
2225+
b = measure r1;
2226+
''',
2227+
[0, 1, 2]
2228+
),
2229+
(
2230+
'''
2231+
bit[1] b;
2232+
qubit[!"1"] r1;
2233+
b = measure r1;
2234+
''',
2235+
[]
2236+
),
2237+
(
2238+
'''
2239+
qubit["1" ^ "0"] r1;
2240+
''',
2241+
[]
2242+
),
2243+
(
2244+
'''
2245+
bit[1] b;
2246+
qubit["1" != "0"] r1;
2247+
b = measure r1;
2248+
''',
2249+
[0]
2250+
),
2251+
(
2252+
'''
2253+
bit[1] b;
2254+
qubit["1" == "0"] r1;
2255+
b = measure r1;
2256+
''',
2257+
[]
2258+
),
2259+
(
2260+
'''
2261+
bit[1] b;
2262+
qubit[1] r1;
2263+
h r1["0" << "1"];
2264+
b = measure r1;
2265+
''',
2266+
[0]
2267+
),
2268+
(
2269+
'''
2270+
bit[2] b;
2271+
qubit[1] r1;
2272+
h r1["0" >> "1"];
2273+
b = measure r1;
2274+
''',
2275+
[0]
2276+
),
2277+
]
2278+
)
2279+
def test_circuit_from_string_literal(qasm, expected):
2280+
try:
2281+
circ = Interpreter().build_circuit(source=qasm)
2282+
assert expected == circ.measured_qubits
2283+
except AttributeError as exc:
2284+
pytest.fail("Interpreter raised an AttributeError")

0 commit comments

Comments
 (0)