Esempio n. 1
0
    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)
Esempio n. 2
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 = 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_expand_state(state, op_wires, device_wires, expected, tol):
    """Test that the expand_state method works as expected."""
    dev = SimulatorDevice(device_wires)
    res = dev._expand_state(state, op_wires)

    assert np.allclose(res, expected, **tol)