コード例 #1
0
ファイル: test_noise.py プロジェクト: qiboteam/qibo
def test_reset_error(backend, density_matrix):

    reset = ResetError(0.8, 0.2)
    noise = NoiseModel()
    noise.add(reset, gates.X, 1)
    noise.add(reset, gates.CNOT)
    noise.add(reset, gates.Z, (0,1))

    circuit = Circuit(3, density_matrix=density_matrix)
    circuit.add(gates.CNOT(0,1))
    circuit.add(gates.Z(1))

    target_circuit = Circuit(3, density_matrix=density_matrix)
    target_circuit.add(gates.CNOT(0,1))
    target_circuit.add(gates.ResetChannel(0, 0.8, 0.2))
    target_circuit.add(gates.ResetChannel(1, 0.8, 0.2))
    target_circuit.add(gates.Z(1))
    target_circuit.add(gates.ResetChannel(1, 0.8, 0.2))

    initial_psi = random_density_matrix(3) if density_matrix else random_state(3)
    np.random.seed(123)
    K.set_seed(123)
    final_state = noise.apply(circuit)(initial_state=np.copy(initial_psi))
    np.random.seed(123)
    K.set_seed(123)
    target_final_state = target_circuit(initial_state=np.copy(initial_psi))

    K.assert_allclose(final_state, target_final_state)
コード例 #2
0
def test_reset_channel(backend):
    initial_rho = random_density_matrix(3)
    gate = gates.ResetChannel(0, p0=0.2, p1=0.2)
    gate.density_matrix = True
    final_rho = gate(K.cast(np.copy(initial_rho)))

    dtype = initial_rho.dtype
    collapsed_rho = np.copy(initial_rho).reshape(6 * (2, ))
    collapsed_rho[0, :, :, 1, :, :] = np.zeros(4 * (2, ), dtype=dtype)
    collapsed_rho[1, :, :, 0, :, :] = np.zeros(4 * (2, ), dtype=dtype)
    collapsed_rho[1, :, :, 1, :, :] = np.zeros(4 * (2, ), dtype=dtype)
    collapsed_rho = collapsed_rho.reshape((8, 8))
    collapsed_rho /= np.trace(collapsed_rho)
    mx = np.kron(np.array([[0, 1], [1, 0]]), np.eye(4))
    flipped_rho = mx.dot(collapsed_rho.dot(mx))
    target_rho = 0.6 * initial_rho + 0.2 * (collapsed_rho + flipped_rho)
    K.assert_allclose(final_rho, target_rho)
コード例 #3
0
def test_reset_channel_repeated(backend):
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)

    initial_state = random_state(5)
    c = Circuit(5)
    c.add(gates.ResetChannel(2, p0=0.3, p1=0.3, seed=123))
    final_state = c(np.copy(initial_state), nshots=30)

    np.random.seed(123)
    target_state = []
    for _ in range(30):
        noiseless_c = Circuit(5)
        if np.random.random() < 0.3:
            noiseless_c.add(gates.Collapse(2))
        if np.random.random() < 0.3:
            noiseless_c.add(gates.Collapse(2))
            noiseless_c.add(gates.X(2))
        target_state.append(noiseless_c(np.copy(initial_state)))
    np.testing.assert_allclose(final_state, target_state)
    qibo.set_backend(original_backend)
コード例 #4
0
def test_reset_channel(backend):
    """Check ``gates.ResetChannel`` on a 3-qubit random density matrix."""
    original_backend = qibo.get_backend()
    qibo.set_backend(backend)
    initial_rho = utils.random_density_matrix(3)
    c = models.Circuit(3, density_matrix=True)
    c.add(gates.ResetChannel(0, p0=0.2, p1=0.2))
    final_rho = c(np.copy(initial_rho))

    dtype = initial_rho.dtype
    collapsed_rho = np.copy(initial_rho).reshape(6 * (2, ))
    collapsed_rho[0, :, :, 1, :, :] = np.zeros(4 * (2, ), dtype=dtype)
    collapsed_rho[1, :, :, 0, :, :] = np.zeros(4 * (2, ), dtype=dtype)
    collapsed_rho[1, :, :, 1, :, :] = np.zeros(4 * (2, ), dtype=dtype)
    collapsed_rho = collapsed_rho.reshape((8, 8))
    collapsed_rho /= np.trace(collapsed_rho)
    mx = np.kron(np.array([[0, 1], [1, 0]]), np.eye(4))
    flipped_rho = mx.dot(collapsed_rho.dot(mx))
    target_rho = 0.6 * initial_rho + 0.2 * (collapsed_rho + flipped_rho)
    np.testing.assert_allclose(final_rho, target_rho)
    qibo.set_backend(original_backend)
コード例 #5
0
def test_reset_channel_repeated(backend):
    initial_state = random_state(5)
    c = Circuit(5)
    c.add(gates.ResetChannel(2, p0=0.3, p1=0.3, seed=123))
    final_state = c(K.cast(np.copy(initial_state)), nshots=30)

    np.random.seed(123)
    target_state = []
    collapse = gates.M(2, collapse=True)
    collapse.nqubits = 5
    xgate = gates.X(2)
    for _ in range(30):
        state = K.cast(np.copy(initial_state))
        if np.random.random() < 0.3:
            state = K.state_vector_collapse(collapse, state, [0])
        if np.random.random() < 0.3:
            state = K.state_vector_collapse(collapse, state, [0])
            state = xgate(state)
        target_state.append(K.copy(state))
    target_state = K.stack(target_state)
    K.assert_allclose(final_state, target_state)