def test_invalid_qubit_state_unitary(self, analytic, shots): """Test that an exception is raised if the unitary matrix is the wrong size""" dev = MixedStateSimulatorDevice(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 = MixedStateSimulatorDevice(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 expected = np.kron(state, state.conj()).reshape([2, 2]) 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 = MixedStateSimulatorDevice(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_identity_basis_state(self, analytic, shots, tol): """Test basis state initialization if identity""" dev = MixedStateSimulatorDevice(4, analytic=analytic, shots=shots) state = np.array([1, 0, 0, 0]) with mimic_execution_for_apply(dev): dev.apply([qml.BasisState(state, wires=[0, 1, 2, 3])]) res = dev._state expected = np.zeros([16]) expected[np.ravel_multi_index(state, [2] * 4)] = 1 expected = np.kron(expected, expected.conj()).reshape([16, 16]) assert np.allclose(res, expected, **tol)
def test_three_qubit_no_parameters(self, init_state, analytic, shots, name, mat, tol): dev = MixedStateSimulatorDevice(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 expected = np.kron(expected, expected.conj()).reshape([8, 8]) assert np.allclose(res, expected, **tol)
def test_qubit_unitary(self, init_state, analytic, shots, mat, tol): N = int(np.log2(len(mat))) dev = MixedStateSimulatorDevice(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 expected = np.kron(expected, expected.conj()).reshape([2**N, 2**N]) assert np.allclose(res, expected, **tol)
def test_two_qubit_no_parameters(self, init_state, shots, name, mat, tol): """Test PauliX application""" dev = MixedStateSimulatorDevice(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 expected = np.kron(expected, expected.conj()).reshape([4, 4]) assert np.allclose(res, expected, **tol)
def test_two_qubits_parameters(self, init_state, analytic, shots, name, func, theta, tol): """Test application of single qubit gates with parameters""" dev = MixedStateSimulatorDevice(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 expected = np.kron(expected, expected.conj()).reshape([4, 4]) assert np.allclose(res, expected, **tol)
def test_rotation(self, init_state, analytic, shots, tol): """Test three axis rotation gate""" dev = MixedStateSimulatorDevice(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 expected = np.kron(expected, expected.conj()).reshape([2, 2]) assert np.allclose(res, expected, **tol)
def simulator_device_1_wire(shots, analytic): """Return a single wire instance of the MixedStateSimulatorDevice class.""" yield MixedStateSimulatorDevice(1, shots=shots, analytic=analytic)
def simulator_device_3_wires(shots): """Return a three wire instance of the MixedStateSimulatorDevice class.""" yield MixedStateSimulatorDevice(3, shots=shots)