Example #1
0
    def test_three_qubit_no_parameters(self, init_state, op, mat, tol):
        dev = QulacsDevice(3)
        state = init_state(3)

        dev.apply([qml.QubitStateVector(state, wires=[0, 1, 2]), op])
        dev._obs_queue = []

        res = dev.state
        expected = mat @ state
        assert np.allclose(res, expected, tol)
Example #2
0
    def test_two_qubit_no_parameters(self, init_state, op, mat, tol):
        """Test PauliX application"""
        dev = QulacsDevice(2)
        state = init_state(2)

        dev.apply([qml.QubitStateVector(state, wires=[0, 1]), op])
        dev._obs_queue = []

        res = dev.state
        expected = mat @ state
        assert np.allclose(res, expected, tol)
Example #3
0
    def test_single_qubit_no_parameters(self, init_state, op, mat, tol):
        """Test PauliX application"""
        dev = QulacsDevice(1)
        state = init_state(1)

        dev.apply([qml.QubitStateVector(state, wires=[0]), op])
        dev._obs_queue = []

        res = np.abs(dev.state)**2
        expected = np.abs(mat @ state)**2
        assert np.allclose(res, expected, tol)
Example #4
0
    def test_single_qubit_parameters(self, init_state, op, func, theta, tol):
        """Test PauliX application"""
        dev = QulacsDevice(1)
        state = init_state(1)

        op.data = [theta]
        dev.apply([qml.QubitStateVector(state, wires=[0]), op])
        dev._obs_queue = []

        res = dev.state
        expected = func(theta) @ state
        assert np.allclose(res, expected, tol)
Example #5
0
    def test_qubit_state_vector(self, init_state, tol):
        """Test QubitStateVector application"""
        dev = QulacsDevice(1)
        state = init_state(1)

        op = qml.QubitStateVector(state, wires=[0])
        dev.apply([op])
        dev._obs_queue = []

        res = dev.state
        expected = state
        assert np.allclose(res, expected, tol)
Example #6
0
    def test_two_qubit_parameters(self, init_state, op, func, theta, tol):
        """Test parametrized two qubit gates application"""
        dev = QulacsDevice(2)
        state = init_state(2)

        op.data = [theta]
        dev.apply([qml.QubitStateVector(state, wires=[0, 1]), op])

        dev._obs_queue = []

        res = np.abs(dev.state)**2
        expected = np.abs(func(theta) @ state)**2
        assert np.allclose(res, expected, tol)
Example #7
0
    def test_qubit_unitary(self, init_state, mat, tol):
        """Test QubitUnitary application"""

        N = int(np.log2(len(mat)))
        dev = QulacsDevice(N)
        state = init_state(N)

        op = qml.QubitUnitary(mat, wires=list(range(N)))
        dev.apply([qml.QubitStateVector(state, wires=list(range(N))), op])
        dev._obs_queue = []

        res = dev.state
        expected = mat @ state
        assert np.allclose(res, expected, tol)
Example #8
0
    def test_qubit_state_vector_on_wires_subset(self, init_state, device_wires,
                                                op_wires, tol):
        """Test QubitStateVector application on a subset of device wires"""
        dev = QulacsDevice(device_wires)
        state = init_state(len(op_wires))

        op = qml.QubitStateVector(state, wires=op_wires)
        dev.apply([op])
        dev._obs_queue = []

        res = dev.state
        expected = dev._expand_state(state, op_wires)

        assert np.allclose(res, expected, tol)
Example #9
0
    def test_basis_state(self, state, tol):
        """Test basis state initialization"""
        dev = QulacsDevice(4)

        op = qml.BasisState(state, wires=[0, 1, 2, 3])
        dev.apply([op])
        dev._obs_queue = []

        res = np.abs(dev.state)**2
        # compute expected probabilities
        expected = np.zeros([2**4])
        expected[np.ravel_multi_index(state, [2] * 4)] = 1

        assert np.allclose(res, expected, tol)
Example #10
0
    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 = QulacsDevice(device_wires)

        op = qml.BasisState(state, wires=op_wires)
        dev.apply([op])
        dev._obs_queue = []

        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)