MCPcopy Create free account
hub / github.com/NVIDIA/cuda-quantum / test_arithmetics

Function test_arithmetics

python/tests/operator/test_matrix_op.py:307–410  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

305
306
307def test_arithmetics():
308 # basic tests for all arithmetic related bindings -
309 # more complex expressions are tested as part of the C++ tests
310 dims = {0: 3, 1: 2}
311 id = identity(0)
312 sum = momentum(0) + position(1)
313 sum_matrix = np.kron(position_matrix(2), identity_matrix(3)) +\
314 np.kron(identity_matrix(2), momentum_matrix(3))
315 assert np.allclose(id.to_matrix(dims), identity_matrix(3))
316 assert np.allclose(sum.to_matrix(dims), sum_matrix)
317 assert np.allclose(
318 (squeeze(0) + displace(1)).to_matrix({
319 0: 2,
320 1: 2
321 },
322 displacement=0.5,
323 squeezing=0.5),
324 [[1.87758256, 0, -0.47942554, 0], [0, 1.87758256, 0, -0.47942554],
325 [0.47942554, 0, 1.87758256, 0], [0, 0.47942554, 0, 1.87758256]])
326
327 # unary operators
328 assert np.allclose((-id).to_matrix(dims), -1. * identity_matrix(3))
329 assert np.allclose((-sum).to_matrix(dims), -1. * sum_matrix)
330 assert np.allclose(id.to_matrix(dims), identity_matrix(3))
331 assert np.allclose(sum.to_matrix(dims), sum_matrix)
332 assert np.allclose((+id).canonicalize().to_matrix(), identity_matrix(1))
333 assert np.allclose((+sum).canonicalize().to_matrix(dims), sum_matrix)
334 assert np.allclose(id.to_matrix(dims), identity_matrix(3))
335
336 # right-hand arithmetics
337 assert np.allclose((id * 2.).to_matrix(dims), 2. * identity_matrix(3))
338 assert np.allclose((sum * 2.).to_matrix(dims), 2. * sum_matrix)
339 assert np.allclose((id * 2.j).to_matrix(dims), 2.j * identity_matrix(3))
340 assert np.allclose((sum * 2.j).to_matrix(dims), 2.j * sum_matrix)
341 assert np.allclose((sum * id).to_matrix(dims), sum_matrix)
342 assert np.allclose((id * sum).to_matrix(dims), sum_matrix)
343 assert np.allclose((id + 2.).to_matrix(dims), 3. * identity_matrix(3))
344 assert np.allclose((sum + 2.).to_matrix(dims),
345 sum_matrix + 2. * identity_matrix(2 * 3))
346 assert np.allclose((id + 2.j).to_matrix(dims),
347 (1. + 2.j) * identity_matrix(3))
348 assert np.allclose((sum + 2.j).to_matrix(dims),
349 sum_matrix + 2.j * identity_matrix(2 * 3))
350 assert np.allclose((sum + id).to_matrix(dims),
351 sum_matrix + identity_matrix(2 * 3))
352 assert np.allclose((id + sum).to_matrix(dims),
353 sum_matrix + identity_matrix(2 * 3))
354 assert np.allclose((id - 2.).to_matrix(dims), -1. * identity_matrix(3))
355 assert np.allclose((sum - 2.).to_matrix(dims),
356 sum_matrix - 2. * identity_matrix(2 * 3))
357 assert np.allclose((id - 2.j).to_matrix(dims),
358 (1. - 2.j) * identity_matrix(3))
359 assert np.allclose((sum - 2.j).to_matrix(dims),
360 sum_matrix - 2.j * identity_matrix(2 * 3))
361 assert np.allclose((sum - id).to_matrix(dims),
362 sum_matrix - identity_matrix(2 * 3))
363 assert np.allclose((id - sum).to_matrix(dims),
364 identity_matrix(2 * 3) - sum_matrix)

Callers

nothing calls this directly

Calls 10

identityFunction · 0.85
identity_matrixFunction · 0.85
squeezeFunction · 0.85
displaceFunction · 0.85
position_matrixFunction · 0.70
momentum_matrixFunction · 0.70
momentumFunction · 0.50
positionFunction · 0.50
to_matrixMethod · 0.45
canonicalizeMethod · 0.45

Tested by

no test coverage detected