def test_invalid_qubit_state_unitary(self, analytic, shots): """Test that an exception is raised if the unitary matrix is the wrong size""" dev = SimulatorDevice(2, analytic=analytic, shots=shots) state = np.array([[0, 123.432], [-0.432, 023.4]]) with pytest.raises(ValueError, match=r"Not a unitary matrix"): with mimic_execution_for_apply(dev): dev.apply([qml.QubitUnitary(state, wires=[0, 1])])
def test_qubit_state_vector(self, init_state, analytic, shots, tol): """Test PauliX application""" dev = SimulatorDevice(1, analytic=analytic, shots=shots) state = init_state(1) with mimic_execution_for_apply(dev): dev.apply([qml.QubitStateVector(state, wires=[0])]) res = dev._state expected = state assert np.allclose(res, expected, **tol)
def test_basis_state(self, analytic, shots, tol): """Test basis state initialization""" dev = SimulatorDevice(4, analytic=analytic, shots=shots) state = np.array([0, 0, 1, 0]) with mimic_execution_for_apply(dev): dev.apply([qml.BasisState(state, wires=[0, 1, 2, 3])]) res = dev._state expected = np.zeros([2**4]) expected[np.ravel_multi_index(state, [2] * 4)] = 1 assert np.allclose(res, expected, **tol)
def test_invalid_qubit_state_vector(self, analytic, shots): """Test that an exception is raised if the state vector is the wrong size""" dev = SimulatorDevice(2, analytic=analytic, shots=shots) state = np.array([0, 123.432]) with pytest.raises( qml.DeviceError, match= r"For QubitStateVector, the state has to be specified for the correct number of qubits", ): with mimic_execution_for_apply(dev): dev.apply([qml.QubitStateVector(state, wires=[0, 1])])
def test_qubit_state_vector_on_wires_subset(self, init_state, device_wires, op_wires, shots, tol): """Test QubitStateVector application on a subset of device wires""" dev = SimulatorDevice(device_wires, shots=shots) state = init_state(len(op_wires)) with mimic_execution_for_apply(dev): dev.apply([qml.QubitStateVector(state, wires=op_wires)]) res = dev.state expected = dev._expand_state(state, op_wires) assert np.allclose(res, expected, **tol)
def test_three_qubit_no_parameters(self, init_state, analytic, shots, name, mat, tol): dev = SimulatorDevice(3, analytic=analytic, shots=shots) state = init_state(3) with mimic_execution_for_apply(dev): dev.apply([ qml.QubitStateVector(state, wires=[0, 1, 2]), qml.__getattribute__(name)(wires=[0, 1, 2]), ]) res = dev._state expected = mat @ state assert np.allclose(res, expected, **tol)
def test_qubit_unitary(self, init_state, analytic, shots, mat, tol): N = int(np.log2(len(mat))) dev = SimulatorDevice(N, analytic=analytic, shots=shots) state = init_state(N) with mimic_execution_for_apply(dev): dev.apply([ qml.QubitStateVector(state, wires=list(range(N))), qml.QubitUnitary(mat, wires=list(range(N))), ]) res = dev._state expected = mat @ state assert np.allclose(res, expected, **tol)
def test_two_qubit_no_parameters(self, init_state, shots, name, mat, tol): """Test PauliX application""" dev = SimulatorDevice(2, shots=shots) state = init_state(2) with mimic_execution_for_apply(dev): dev.apply([ qml.QubitStateVector(state, wires=[0, 1]), qml.__getattribute__(name)(wires=[0, 1]), ]) res = dev._state expected = mat @ state assert np.allclose(res, expected, **tol)
def test_apply_cphase(self, tol, par, input, shots): """Tests that applying the CPhase gate yields the expected output.""" device = SimulatorDevice(2, shots=shots) cphase_mat = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, np.exp(1j * par)]]) expected = cphase_mat @ input device.reset() device._initial_state = np.array(input, dtype=np.complex64) device.apply([ops.CPhase(par, wires=[0, 1])]) assert np.allclose(device.state, expected, **tol)
def test_apply_iswap(self, tol, input, shots): """Tests that applying the iSWAP gate yields the expected output.""" device = SimulatorDevice(2, shots=shots) iswap_mat = np.array([[1, 0, 0, 0], [0, 0, 1j, 0], [0, 1j, 0, 0], [0, 0, 0, 1]]) expected = iswap_mat @ input device.reset() device._initial_state = np.array(input, dtype=np.complex64) device.apply([ops.ISWAP(wires=[0, 1])]) assert np.allclose(device.state, expected, **tol)
def test_two_qubits_parameters(self, init_state, analytic, shots, name, func, theta, tol): """Test application of two qubit gates with parameters""" dev = SimulatorDevice(2, analytic=analytic, shots=shots) state = init_state(2) with mimic_execution_for_apply(dev): dev.apply([ qml.QubitStateVector(state, wires=[0, 1]), qml.__getattribute__(name)(theta, wires=[0, 1]), ]) res = dev._state expected = func(theta) @ state assert np.allclose(res, expected, **tol)
def test_single_qubit_no_parameters(self, init_state, analytic, shots, name, mat, tol): """Test application of single qubit gates without parameters""" dev = SimulatorDevice(1, analytic=analytic, shots=shots) state = init_state(1) with mimic_execution_for_apply(dev): dev.apply([ qml.QubitStateVector(state, wires=[0]), qml.__getattribute__(name)(wires=[0]), ]) res = dev._state expected = mat @ state assert np.allclose(res, expected, **tol)
def test_basis_state_on_wires_subset(self, state, device_wires, op_wires, tol): """Test basis state initialization on a subset of device wires""" dev = SimulatorDevice(device_wires) with mimic_execution_for_apply(dev): dev.apply([qml.BasisState(state, wires=op_wires)]) res = np.abs(dev.state)**2 # compute expected probabilities expected = np.zeros([2**len(op_wires)]) expected[np.ravel_multi_index(state, [2] * len(op_wires))] = 1 expected = dev._expand_state(expected, op_wires) assert np.allclose(res, expected, **tol)
def test_rotation(self, init_state, analytic, shots, tol): """Test three axis rotation gate""" dev = SimulatorDevice(1, analytic=analytic, shots=shots) state = init_state(1) a = 0.542 b = 1.3432 c = -0.654 with mimic_execution_for_apply(dev): dev.apply([ qml.QubitStateVector(state, wires=[0]), qml.Rot(a, b, c, wires=[0]) ]) res = dev._state expected = rot(a, b, c) @ state assert np.allclose(res, expected, **tol)