def test_krauss_channel_errors(backend): # bad Kraus matrix shape a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) with pytest.raises(ValueError): gate = gates.KrausChannel([((0, 1), a1)]) # Using KrausChannel on state vectors channel = gates.KrausChannel([((0, ), np.eye(2))]) with pytest.raises(ValueError): channel._state_vector_call(np.random.random(4)) # Attempt to construct unitary for KrausChannel with pytest.raises(ValueError): channel._construct_unitary()
def test_krauss_channel_errors(backend): """Test errors raised by `gates.KrausChannel`.""" original_backend = qibo.get_backend() qibo.set_backend(backend) # bad Kraus matrix shape a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) with pytest.raises(ValueError): gate = gates.KrausChannel([((0, 1), a1)]) # Using KrausChannel on state vectors channel = gates.KrausChannel([((0, ), np.eye(2))]) with pytest.raises(ValueError): channel.state_vector_call(np.random.random(4)) # Attempt to construct unitary for KrausChannel with pytest.raises(ValueError): channel.construct_unitary() qibo.set_backend(original_backend)
def test_general_channel(backend, tfmatrices, oncircuit): """Test `gates.KrausChannel`.""" original_backend = qibo.get_backend() qibo.set_backend(backend) initial_rho = utils.random_density_matrix(2) a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) if tfmatrices: from qibo import K a1, a2 = K.cast(a1), K.cast(a2) gate = gates.KrausChannel([((1, ), a1), ((0, 1), a2)]) assert gate.target_qubits == (0, 1) if oncircuit: c = models.Circuit(2, density_matrix=True) c.add(gate) final_rho = c(np.copy(initial_rho)) else: final_rho = gate(np.copy(initial_rho)) m1 = np.kron(np.eye(2), np.array(a1)) m2 = np.array(a2) target_rho = (m1.dot(initial_rho).dot(m1.conj().T) + m2.dot(initial_rho).dot(m2.conj().T)) np.testing.assert_allclose(final_rho, target_rho) qibo.set_backend(original_backend)
def test_general_channel(backend, tfmatrices, oncircuit): """Test `gates.KrausChannel`.""" original_backend = qibo.get_backend() qibo.set_backend(backend) initial_rho = utils.random_density_matrix(2) a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) if tfmatrices: import tensorflow as tf from qibo.config import DTYPES a1 = tf.cast(a1, dtype=DTYPES.get('DTYPECPX')) a2 = tf.cast(a2, dtype=DTYPES.get('DTYPECPX')) gate = gates.KrausChannel([((1, ), a1), ((0, 1), a2)]) assert gate.target_qubits == (0, 1) if oncircuit: c = models.Circuit(2, density_matrix=True) c.add(gate) final_rho = c(np.copy(initial_rho)).numpy() else: if backend == "custom": final_rho = gate(np.copy(initial_rho)) else: final_rho = gate(np.copy(initial_rho).reshape(4 * (2, ))) final_rho = final_rho.numpy().reshape((4, 4)) m1 = np.kron(np.eye(2), np.array(a1)) m2 = np.array(a2) target_rho = (m1.dot(initial_rho).dot(m1.conj().T) + m2.dot(initial_rho).dot(m2.conj().T)) np.testing.assert_allclose(final_rho, target_rho) qibo.set_backend(original_backend)
def test_density_matrix_circuit_errors(): """Check errors of circuits that simulate density matrices.""" # Attempt to distribute density matrix circuit with pytest.raises(NotImplementedError): c = models.Circuit(5, accelerators={"/GPU:0": 2}, density_matrix=True) # Attempt to add Kraus channel to non-density matrix circuit c = models.Circuit(5) with pytest.raises(ValueError): c.add(gates.KrausChannel([((0, ), np.eye(2))]))
def test_controlled_by_channel_error(): with pytest.raises(ValueError): gates.PauliNoiseChannel(0, px=0.5).controlled_by(1) a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) config = [((1, ), a1), ((0, 1), a2)] with pytest.raises(ValueError): gates.KrausChannel(config).controlled_by(1)
def test_controlled_by_channel(): """Test that attempting to control channels raises error.""" c = models.Circuit(2, density_matrix=True) with pytest.raises(ValueError): c.add(gates.PauliNoiseChannel(0, px=0.5).controlled_by(1)) a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) config = [((1, ), a1), ((0, 1), a2)] with pytest.raises(ValueError): gates.KrausChannel(config).controlled_by(1)
def test_density_matrix_circuit_errors(): """Check errors of circuits that simulate density matrices.""" # Switch `gate.density_matrix` to `True` after setting `nqubits` gate = gates.X(0) gate.nqubits = 2 with pytest.raises(RuntimeError): gate.density_matrix = True # Attempt to distribute density matrix circuit with pytest.raises(NotImplementedError): c = models.Circuit(5, accelerators={"/GPU:0": 2}, density_matrix=True) # Attempt to add Kraus channel to non-density matrix circuit c = models.Circuit(5) with pytest.raises(ValueError): c.add(gates.KrausChannel([((0, ), np.eye(2))]))
def test_general_channel(backend): a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) a1, a2 = K.cast(a1), K.cast(a2) initial_rho = random_density_matrix(2) gate = gates.KrausChannel([((1, ), a1), ((0, 1), a2)]) assert gate.target_qubits == (0, 1) final_rho = gate(np.copy(initial_rho)) m1 = np.kron(np.eye(2), K.to_numpy(a1)) m2 = K.to_numpy(a2) target_rho = (m1.dot(initial_rho).dot(m1.conj().T) + m2.dot(initial_rho).dot(m2.conj().T)) K.assert_allclose(final_rho, target_rho)
def test_channel_gate_setters(backend): a1 = np.sqrt(0.4) * np.array([[0, 1], [1, 0]]) a2 = np.sqrt(0.6) * np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]) channel = gates.KrausChannel([((1,), a1), ((0, 1), a2)]) _ = channel.inverse_gates # create inverse gates channel.nqubits = 5 channel.density_matrix = True for gate in channel.gates: assert gate.nqubits == 5 assert gate.density_matrix for gate in channel.inverse_gates: if gate is not None: assert gate.nqubits == 5 assert gate.density_matrix