@@ -1149,7 +1149,7 @@ def test_qiskit2tc():
1149
1149
np .testing .assert_allclose (qis_unitary2 , qis_unitary , atol = 1e-5 )
1150
1150
1151
1151
1152
- @pytest .mark .parametrize ("backend" , [lf ("tfb" ), lf ("jaxb" )])
1152
+ @pytest .mark .parametrize ("backend" , [lf ("tfb" ), lf ("jaxb" ), lf ( "torchb" ) ])
1153
1153
def test_qiskit2tc_parameterized (backend ):
1154
1154
try :
1155
1155
from qiskit .circuit import Parameter , ParameterVector , QuantumCircuit
@@ -1171,11 +1171,12 @@ def test_qiskit2tc_parameterized(backend):
1171
1171
ansatz4_param = ParameterVector ("φ" , 3 )
1172
1172
ansatz4 .rx (2.0 * ansatz4_param [0 ] + 5.0 , 0 )
1173
1173
ansatz4 .ry (ansatz4_param [0 ] * ansatz4_param [1 ] + ansatz4_param [2 ], 0 )
1174
- ansatz4 .rz (
1175
- np .exp (np .sin (ansatz4_param [0 ]))
1176
- + np .abs (ansatz4_param [1 ]) / np .arctan (ansatz4_param [2 ]),
1177
- 0 ,
1178
- )
1174
+ if tc .backend .name != "pytorch" : # pytorch backend with ufuncs is not supported
1175
+ ansatz4 .rz (
1176
+ np .exp (np .sin (ansatz4_param [0 ]))
1177
+ + np .abs (ansatz4_param [1 ]) / np .arctan (ansatz4_param [2 ]),
1178
+ 0 ,
1179
+ )
1179
1180
ansatz_list = [ansatz1 , ansatz2 , ansatz3 , ansatz4 ]
1180
1181
for ansatz in ansatz_list :
1181
1182
n = ansatz .num_qubits
@@ -1196,7 +1197,7 @@ def get_unitary(params):
1196
1197
1197
1198
tc_unitary = get_unitary (params )
1198
1199
tc_unitary = np .reshape (tc_unitary , [2 ** n , 2 ** n ])
1199
- p_mat = perm_matrix (n )
1200
+ p_mat = tc . array_to_tensor ( perm_matrix (n ) )
1200
1201
np .testing .assert_allclose (
1201
1202
p_mat @ tc_unitary @ p_mat , qiskit_unitary , atol = 1e-5
1202
1203
)
@@ -1207,7 +1208,7 @@ def cost_fn(params):
1207
1208
1208
1209
if ansatz in [ansatz1 , ansatz2 , ansatz4 ]:
1209
1210
grad = tc .backend .grad (cost_fn )(tc .backend .convert_to_tensor (params ))
1210
- assert np . sum (np .isnan (grad )) == 0
1211
+ assert tc . backend . sum (tc . num_to_tensor ( np .isnan (grad ) )) == 0
1211
1212
else :
1212
1213
# tf only supports tf tensor as input
1213
1214
grad = tc .backend .grad (cost_fn )(
0 commit comments