def test_qubit_state_vector(self, init_state, tol, rep): """Test qubit state vector application""" dev = DefaultTensorTF(wires=1, representation=rep) state = init_state(1) dev.execute([qml.QubitStateVector(state, wires=[0])], [], {}) res = dev._state().numpy().flatten() expected = state assert np.allclose(res, expected, atol=tol, rtol=0)
def test_basis_state(self, tol, rep): """Test basis state initialization""" dev = DefaultTensorTF(wires=4, representation=rep) state = np.array([0, 0, 1, 0]) dev.execute([qml.BasisState(state, wires=[0, 1, 2, 3])], [], {}) res = dev._state().numpy().flatten() expected = np.zeros([2**4]) expected[np.ravel_multi_index(state, [2] * 4)] = 1 assert np.allclose(res, expected, atol=tol, rtol=0)
def test_two_qubit_parameters(self, init_state, op, func, theta, rep, tol): """Test two qubit parametrized operations""" dev = DefaultTensorTF(wires=2, representation=rep) state = init_state(2) queue = [qml.QubitStateVector(state, wires=[0, 1])] queue += [op(theta, wires=[0, 1])] dev.execute(queue, [], {}) res = dev._state().numpy().flatten() expected = func(theta) @ state assert np.allclose(res, expected, atol=tol, rtol=0)
def test_single_qubit_no_parameters(self, init_state, op, mat, rep, tol): """Test non-parametrized single qubit operations""" dev = DefaultTensorTF(wires=1, representation=rep) state = init_state(1) queue = [qml.QubitStateVector(state, wires=[0])] queue += [op(wires=0)] dev.execute(queue, [], {}) res = dev._state().numpy().flatten() expected = mat @ state assert np.allclose(res, expected, atol=tol, rtol=0)
def test_qubit_unitary(self, init_state, mat, rep, tol): """Test application of arbitrary qubit unitaries""" N = int(np.log2(len(mat))) dev = DefaultTensorTF(wires=N, representation=rep) state = init_state(N) queue = [qml.QubitStateVector(state, wires=range(N))] queue += [qml.QubitUnitary(mat, wires=range(N))] dev.execute(queue, [], {}) res = dev._state().numpy().flatten() expected = mat @ state assert np.allclose(res, expected, atol=tol, rtol=0)
def test_rotation(self, init_state, rep, tol): """Test three axis rotation gate""" dev = DefaultTensorTF(wires=1, representation=rep) state = init_state(1) a = 0.542 b = 1.3432 c = -0.654 queue = [qml.QubitStateVector(state, wires=[0])] queue += [qml.Rot(a, b, c, wires=0)] dev.execute(queue, [], {}) res = dev._state().numpy().flatten() expected = rot(a, b, c) @ state assert np.allclose(res, expected, atol=tol, rtol=0)
def test_three_qubit_no_parameters(self, init_state, op, mat, rep, tol): """Test non-parametrized three qubit operations""" if rep == "mps": pytest.skip("Three-qubit gates not supported for `mps` representation.") dev = DefaultTensorTF(wires=3, representation=rep) state = init_state(3) queue = [qml.QubitStateVector(state, wires=[0, 1, 2])] queue += [op(wires=[0, 1, 2])] dev.execute(queue, [], {}) res = dev._state().numpy().flatten() expected = mat @ state assert np.allclose(res, expected, atol=tol, rtol=0)