File tree 3 files changed +93
-6
lines changed
src/braket/default_simulator/openqasm
test/unit_tests/braket/default_simulator/openqasm
3 files changed +93
-6
lines changed Original file line number Diff line number Diff line change 98
98
[BooleanLiteral (xv .value ^ yv .value ) for xv , yv in zip (x .values , y .values )]
99
99
),
100
100
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 )]
102
104
),
103
105
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 ))]
107
+ + x .values [: len (x .values ) - len (y .values )]
108
+ if isinstance (y , ArrayLiteral )
109
+ else [BooleanLiteral (False ) for _ in range (y .value )]
110
+ + x .values [: len (x .values ) - y .value ]
105
111
),
106
112
getattr (UnaryOperator , "~" ): lambda x : ArrayLiteral (
107
113
[BooleanLiteral (not v .value ) for v in x .values ]
Original file line number Diff line number Diff line change @@ -247,8 +247,13 @@ def _(self, node: Identifier) -> LiteralType:
247
247
248
248
@visit .register
249
249
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 )
252
257
253
258
@visit .register
254
259
def _ (self , node : QuantumReset ) -> None :
Original file line number Diff line number Diff line change 1
- # Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2
- #
1
+ # Copyright Amazon.com Inc. or its affiliates. All Rights Reserved #
3
2
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
3
# may not use this file except in compliance with the License. A copy of
5
4
# the License is located at
@@ -2207,3 +2206,80 @@ def test_measure_invalid_qubit():
2207
2206
def test_measure_qubit_out_of_range (qasm , expected ):
2208
2207
with pytest .raises (IndexError , match = expected ):
2209
2208
Interpreter ().build_circuit (qasm )
2209
+
2210
+
2211
+ @pytest .mark .parametrize (
2212
+ "qasm, expected" ,
2213
+ [
2214
+ (
2215
+ """
2216
+ bit[2] b;
2217
+ qubit["10"] r1;
2218
+ b = measure r1;
2219
+ """ ,
2220
+ [0 , 1 ],
2221
+ ),
2222
+ (
2223
+ """
2224
+ bit[3] b;
2225
+ qubit["11"] r1;
2226
+ b = measure r1;
2227
+ """ ,
2228
+ [0 , 1 , 2 ],
2229
+ ),
2230
+ (
2231
+ """
2232
+ bit[1] b;
2233
+ qubit[!"1"] r1;
2234
+ b = measure r1;
2235
+ """ ,
2236
+ [],
2237
+ ),
2238
+ (
2239
+ """
2240
+ qubit["1" ^ "0"] r1;
2241
+ """ ,
2242
+ [],
2243
+ ),
2244
+ (
2245
+ """
2246
+ bit[1] b;
2247
+ qubit["1" != "0"] r1;
2248
+ b = measure r1;
2249
+ """ ,
2250
+ [0 ],
2251
+ ),
2252
+ (
2253
+ """
2254
+ bit[1] b;
2255
+ qubit["1" == "0"] r1;
2256
+ b = measure r1;
2257
+ """ ,
2258
+ [],
2259
+ ),
2260
+ (
2261
+ """
2262
+ bit[1] b;
2263
+ qubit[1] r1;
2264
+ h r1["0" << "1"];
2265
+ b = measure r1;
2266
+ """ ,
2267
+ [0 ],
2268
+ ),
2269
+ (
2270
+ """
2271
+ bit[2] b;
2272
+ qubit[1] r1;
2273
+ h r1["0" >> "1"];
2274
+ b = measure r1;
2275
+ """ ,
2276
+ [0 ],
2277
+ ),
2278
+ ],
2279
+ )
2280
+ def test_circuit_from_string_literal (qasm , expected ):
2281
+ try :
2282
+ circ = Interpreter ().build_circuit (source = qasm )
2283
+ assert expected == circ .measured_qubits
2284
+ except AttributeError as exc :
2285
+ pytest .fail ("Interpreter raised an AttributeError" )
You can’t perform that action at this time.
0 commit comments