def test_sparse_krauss_versus_dense_krauss(): r"""Test the sparse krauss operators versus dense krauss operators.""" p, q = 0.2, 0.4 k_ops = initialize_dephrasure_examples(p, q) spkrauss = SparseKraus(k_ops, [1, 1], 2, 3) dKrauss = DenseKraus(k_ops, [1, 1], 2, 3) for n in range(1, 4): # Update channel to correpond to n. spkrauss.update_kraus_operators(n) dKrauss.update_kraus_operators(n) # Test nth krauss operators assert issubclass(type(spkrauss.nth_kraus_ops), SparseArray) spkrauss_nth_krauss = spkrauss.nth_kraus_ops.todense() dkrauss_nth_krauss = dKrauss.nth_kraus_ops assert_array_almost_equal(spkrauss_nth_krauss, dkrauss_nth_krauss) # Test exchange array assert issubclass(type(spkrauss.nth_kraus_exch), SparseArray) spkrauss_exchange = spkrauss.nth_kraus_exch.todense() dkrauss_exchange = dKrauss.nth_kraus_exch assert_array_almost_equal(dkrauss_exchange, spkrauss_exchange) # Test channel output rho = np.array(rand_dm_ginibre(2**n).data.todense()) assert_array_almost_equal(spkrauss.channel(rho), dKrauss.channel(rho)) # Test entropy exchange assert_array_almost_equal( spkrauss.entropy_exchange(rho, n)[0], dKrauss.entropy_exchange(rho, n)[0])
def test_channel_method_using_dephrasure_dense(): r"""Test the channel from 'krauss.DenseKraus.channel' using dephrasure example.""" p, q = 0.2, 0.4 k_ops = initialize_dephrasure_examples(p, q) krauss_ops = np.array(k_ops).copy() dkrauss = DenseKraus(k_ops, [1, 1], 2, 3) assert_raises(AssertionError, dkrauss.channel, [np.eye(2)]) for n in range(1, 4): if n != 1: krauss_ops = np.kron(np.array(k_ops), krauss_ops) # Get random density state. rho = np.array(rand_dm_ginibre(2**n).data.todense()) assert np.all(krauss_ops.shape[2] == rho.shape[0]) # Multiply each krauss operators individually desired = np.zeros((3**n, 3**n), dtype=np.complex128) for krauss in krauss_ops: temp = krauss.dot(rho).dot(krauss.T.conj()) desired += temp dkrauss.update_kraus_operators(n) rho = np.array([rho]) actual = dkrauss.channel(rho) assert_array_almost_equal(actual[0], desired)
def test_adjoint_of_a_channel(): r"""Test the adjoint of the channel on DenseKraus and SparseKraus.""" p1, p2, p3 = 0.1, 0.01, 0.4 krauss_ops = initialize_pauli_examples(p1, p2, p3) # Dense Krauss dense_krauss_obj = DenseKraus(krauss_ops, [1, 1], 2, 2) sparse_krauss_obj = SparseKraus(krauss_ops, [1, 1], 2, 2) for n in range(1, 3): # Update Krauss Operators k_ops = krauss_ops.copy() if n != 1: k_ops = np.kron(np.array(k_ops), krauss_ops) dense_krauss_obj.update_kraus_operators(n) sparse_krauss_obj.update_kraus_operators(n) # Test on random density matrices for _ in range(0, 10): rho = np.array(rand_dm_ginibre(2**n).data.todense()) desired = np.zeros((2**n, 2**n), dtype=np.complex128) for k in k_ops: desired += np.conj(k.T).dot(rho.dot(k)) dense_chan = dense_krauss_obj.channel(rho, True) sparse_chan = sparse_krauss_obj.channel(rho, True) assert_array_almost_equal(desired, dense_chan) assert_array_almost_equal(desired, sparse_chan)
def test_adjoint_of_complementary_channel_kraus_operators(): r"""Test the adjoint channel of the complementary channel based on kraus operators.""" # Test Adjoint complementary channel is unital rho = np.eye(4) krauss_ops = initialize_dephrasure_examples(0.1, 0.2) chan = DenseKraus(krauss_ops, [1, 1], 2, 3, orthogonal_kraus=[2]) chan_s = SparseKraus(krauss_ops, [1, 1], 2, 3) desired = np.eye(2) # Test Dense Kraus actual = chan.entropy_exchange(rho, 1, adjoint=True) assert np.all(np.abs(actual - desired) < 1e-4) # Test Sparse Kraus actual = chan_s.entropy_exchange(rho, 1, adjoint=True).todense() assert np.all(np.abs(actual - desired) < 1e-4) # Test based on random density matrix. rho = rand_dm_ginibre(4, 4).data.toarray() desired = sum([ rho[i, j] * krauss_ops[i].T.dot(krauss_ops[j]) for i in range(0, 4) for j in range(0, 4) ]) actual = chan.entropy_exchange(rho, 1, adjoint=True) assert np.all(np.abs(actual - desired) < 1e-4) actual = chan_s.entropy_exchange(rho, 1, adjoint=True).todense() assert np.all(np.abs(actual - desired) < 1e-4)
def test_channel_method_using_dephrasure(): r"""Test the method 'channel' from 'QCorr.channel.AnalyticQChan' using dephrasure kraus ops.""" p, q = 0.2, 0.4 single_krauss_ops = set_up_dephrasure_conditions(p, q) orthogonal_krauss_indices = [2] for sparse in [False, True]: krauss_ops = single_krauss_ops.copy() channel = AnalyticQChan(single_krauss_ops, [1, 1], 2, 3, orthogonal_krauss_indices, sparse) for n in range(1, 4): if n != 1: krauss_ops = np.kron(single_krauss_ops, krauss_ops) # Get random density state. rho = np.array(rand_dm_ginibre(2**n).data.todense()) # Test channel method using kraus perators matches 'channel.channel' desired = np.zeros((3**n, 3**n), dtype=np.complex128) for krauss in krauss_ops: temp = krauss.dot(rho).dot(krauss.T) desired += temp actual = channel.channel(rho, n) assert_array_almost_equal(actual, desired) # Test constructor accepts the right input. assert_raises(TypeError, AnalyticQChan, "asdsa", [1, 1], 2, 2)
def test_rand_dm_ginibre_rank(): """ Random Qobjs: Ginibre-random density ops have correct rank. """ random_qobj = rand_dm_ginibre(5, rank=3) rank = sum([abs(E) >= 1e-10 for E in random_qobj.eigenenergies()]) assert rank == 3
def test_entropy_exchange_dephrasure_channel(): r"""Test the entropy exchange of dephrasure channel using kraus operators.""" p, q = 0.2, 0.4 single_krauss_ops = set_up_dephrasure_conditions(p, q) orthogonal_krauss_indices = [2] # Test both sparse and non-sparse. for issparse in [True, False]: krauss_ops = single_krauss_ops.copy() channel = AnalyticQChan(single_krauss_ops, [1, 1], 2, 3, orthogonal_krauss_indices, sparse=issparse) for n in range(1, 4): if n != 1: krauss_ops = np.kron(single_krauss_ops, krauss_ops) # Get random density state. rho = np.array(rand_dm_ginibre(2**n).data.todense()) if issparse: actual = channel.entropy_exchange(rho, n)[0] else: actual = channel.entropy_exchange(rho, n) actual = block_diag(actual[0], actual[1]) assert actual.shape == (4**n, 4**n) for i in range(0, 4**n): for j in range(0, 4**n): desired = np.trace(krauss_ops[i].dot( rho.dot(np.conjugate(krauss_ops[j]).T))) assert np.abs(actual[i, j] - desired) < 1e-10
def test_entanglement_fidelity(): r"""Test the average_entanglement_fidelity method of both Sparse and Dense Kraus Operators.""" # Get krauss operators from dephrasure channel krauss_ops = initialize_dephrasure_examples(0.1, 0.2) chan = DenseKraus(krauss_ops, [1, 1], 2, 3) chan_sp = SparseKraus(krauss_ops, [1, 1], 2, 3) desired_fid = 0. probs = [0.25, 0.75] states = [ np.array(rand_dm_ginibre(2).data.todense()), np.array(rand_dm_ginibre(2).data.todense()) ] for i, p in enumerate(probs): for k in krauss_ops: desired_fid += p * np.abs(np.trace(k.dot(states[i])))**2 assert np.abs(desired_fid - chan.average_entanglement_fidelity(probs, states)) assert np.abs(desired_fid - chan_sp.average_entanglement_fidelity(probs, states))
def imperfect_state(coeff, error=0.2, H_rand=False, with_phase=False): s = state(*coeff) if H_rand == False: H_rand = q.rand_dm_ginibre(len(coeff)) H_rand = H_rand / H_rand.norm() H_rand = q.Qobj( lin.block_diag(H_rand[:], np.zeros((N - len(coeff), N - len(coeff))))) s_e = q.sesolve(H_rand, s, [0., error]).states[1] if with_phase: return s_e else: return list(np.ndarray.flatten(np.abs(s_e[:len(coeff)])**2))
def test_action_of_choi_operator(): r"""Test that choi operator matches well-known krauss operators.""" krauss = initialize_pauli_examples(0.1, 0.2, 0.3) choi = sum([ np.outer(np.ravel(x, "F"), np.conj(np.ravel(x, "F").T)) for x in krauss ]) choi_obj = ChoiQutip(choi, numb_qubits=[1, 1], dim_in=2, dim_out=2) for _ in range(0, 1000): rho = np.array(rand_dm_ginibre(2).data.todense()) actual = choi_obj.channel(rho) desired = sum([k.dot(rho).dot(np.conj(k).T) for k in krauss]) assert np.all(np.abs(actual - desired) < 1e-3) # Test number of qubits being 2. krauss = np.kron(krauss, krauss) choi = sum([ np.outer(np.ravel(x, "F"), np.conj(np.ravel(x, "F"))) for x in krauss ]) choi_obj = ChoiQutip(choi, numb_qubits=[2, 2], dim_in=2, dim_out=2) for _ in range(0, 1000): rho = np.array(rand_dm_ginibre(4).data.todense()) actual = choi_obj.channel(rho) desired = sum([k.dot(rho).dot(np.conj(k).T) for k in krauss]) assert np.all(np.abs(actual - desired) < 1e-3) # Test Dephrasure Channe krauss = set_up_dephrasure_conditions(0.1, 0.2) choi = sum([ np.outer(np.ravel(x, "F"), np.conj(np.ravel(x, "F"))) for x in krauss ]) choi_obj = ChoiQutip(choi, [1, 1], 2, 3) for _ in range(0, 1000): rho = np.array(rand_dm_ginibre(2).data.todense()) actual = choi_obj.channel(rho) desired = sum([k.dot(rho).dot(np.conj(k).T) for k in krauss]) assert np.all(np.abs(actual - desired) < 1e-3)
def check_two_sets_of_krauss_are_same(krauss1, krauss2, numb=1000): is_same = True chann1 = AnalyticQChan(krauss1, [1, 1], 2, 2) chann2 = AnalyticQChan(krauss2, [1, 1], 2, 2) for _ in range(0, numb): # Get random Rho rho = np.array(rand_dm_ginibre(2).data.todense()) rho1 = chann1.channel(rho, 1) rho2 = chann2.channel(rho, 1) # Compare them if np.any(np.abs(rho1 - rho2) > 1e-3): is_same = False break return is_same
def test_channel_with_amplitude_damping_channel(): r"""Test channel method of kraus operators with ampltitude damping example.""" n = 1 for err in np.arange(0.0, 1., 0.01): krauss_1 = np.array([[1., 0.], [0, np.sqrt(1 - err)]]) krauss_2 = np.array([[0, np.sqrt(err)], [0., 0]]) krauss_ops = [krauss_1, krauss_2] krauss = DenseKraus(krauss_ops, [1, 1], 2, 2) for _ in range(0, 10): rho = np.array(rand_dm_ginibre(2**n).data.todense()) channel = krauss_1.dot(rho).dot(krauss_1.conj().T) channel += krauss_2.dot(rho).dot(krauss_2.conj().T) actual = krauss.channel(rho) assert_array_almost_equal(actual, channel)
def test_orthogonal_krauss_conditions(): r"""Test that orthogonal kraus conds splits entropy exchange matrix and matches without it.""" p, q = 0.2, 0.4 k_ops = initialize_dephrasure_examples(p, q) dkrauss = DenseKraus(k_ops, [1, 1], 2, 3) dKrauss_cond = DenseKraus(k_ops, [1, 1], 2, 3, orthogonal_kraus=[2]) for n in range(1, 4): dkrauss.update_kraus_operators(n) dKrauss_cond.update_kraus_operators(n) for _ in range(0, 50): rho = np.array(rand_dm_ginibre(2**n).data.todense()) entropy_exchange = dkrauss.entropy_exchange(rho, n) entropy_exchange2 = dKrauss_cond.entropy_exchange(rho, n) assert_array_almost_equal( entropy_exchange[0], block_diag(entropy_exchange2[0], entropy_exchange2[1]))
def check_two_sets_of_krauss_are_same(krauss1, krauss2, numb_qubits, dim_in, dim_out, numb=1000): r"""Helper function for checking if two kraus operators by checking if they agree.""" is_same = True chann1 = DenseKraus(krauss1, numb_qubits, dim_in, dim_out) chann2 = DenseKraus(krauss2, numb_qubits, dim_in, dim_out) for _ in range(0, numb): # Get random Rho rho = np.array(rand_dm_ginibre(2).data.todense()) rho1 = chann1.channel(rho) rho2 = chann2.channel(rho) # Compare them if np.any(np.abs(rho1 - rho2) > 1e-3): is_same = False break return is_same
def entropy_exchange_helper_assertion(krauss, single_krauss_ops): # Helps check that entropy exchange matrix entries matches definition. krauss_ops = np.array(single_krauss_ops).copy() for n in range(1, 4): krauss.update_kraus_operators(n) if n != 1: krauss_ops = np.kron(single_krauss_ops, krauss_ops) # Get random density state. rho = np.array(rand_dm_ginibre(2**n).data.todense()) assert np.all(krauss_ops.shape[2] == rho.shape[0]) actual = krauss.entropy_exchange(rho, n)[0] assert actual.shape == (4**n, 4**n) for i in range(0, 4**n): for j in range(0, 4**n): desired = np.trace(krauss_ops[i].dot( rho.dot(np.conjugate(krauss_ops[j]).T))) assert np.abs(actual[i, j] - desired) < 1e-10
def test_entropy_exchange_matrix_with_dephasing_channel(): r"""Test that entropy exchange matches dephasing channel example.""" n = 1 for err in np.arange(0.0, 1., 0.01): krauss_1 = np.array([[1., 0.], [0, np.sqrt(1 - err)]]) krauss_2 = np.array([[0, np.sqrt(err)], [0., 0]]) krauss_ops = [krauss_1, krauss_2] krauss = DenseKraus(krauss_ops, [1, 1], 2, 2) rho = np.array(rand_dm_ginibre(2**n).data.todense()) W = np.array([[ np.trace(krauss_1.dot(rho).dot(krauss_1.conj().T)), np.trace(krauss_1.dot(rho).dot(krauss_2.conj().T)) ], [ np.trace(krauss_2.dot(rho).dot(krauss_1.conj().T)), np.trace(krauss_2.dot(rho).dot(krauss_2.conj().T)) ]]) actual = krauss.entropy_exchange(rho, n)[0] assert_array_almost_equal(actual, W)
def test_entropy_complementary_channel_of_choi_operator(): r"""Test the entropy of complementary channel of the choi operator matches entropy exchange.""" # Dephrasure krauss = set_up_dephrasure_conditions(0.1, 0.2) choi = sum([ np.outer(np.ravel(x, "F"), np.conj(np.ravel(x, "F"))) for x in krauss ]) choi_obj = ChoiQutip(choi, [1, 1], 2, 3) for _ in range(0, 1000): # Random rho rho = rand_dm_ginibre(2).data.todense() # Get eigenvalues desired = np.linalg.eigvalsh(entropyexchange(krauss, rho)) actual = np.linalg.eigvalsh(choi_obj.complementary_channel(rho)) # Assert that eigenvalues are the same. i = 0 for x in actual: if np.abs(x) > 1e-8: assert np.abs(desired[i] - x) < 1e-5 i += 1
def plot_func_sensitivity(epsilon_max, N_err=5, step_size=0.01): global system global m_state N_ = 3 n = 3 s = 0 max_coh = list((1. / N_) * np.ones(N_)) opt_res = find_max_func_half(N_, max_coh, [], False, s, n) low_bound = -find_max_func_half( N_ - 1, list( (N_ / (N_ - 1)) * np.array(max_coh)[:-1]), [], False, s, n)['fun'] meas_coeff = opt_res['x'] epsilon_list = np.linspace(0, epsilon_max, int(epsilon_max / step_size)) alpha_list = np.empty((N_err, int(epsilon_max / step_size))) val_list = np.empty((N_err, int(epsilon_max / step_size))) for i in range(N_err): H_rand = q.rand_dm_ginibre(N_, 1) for j in range(len(epsilon_list)): meas_coeff_err = imperfect_state(meas_coeff, epsilon_list[j], H_rand, with_phase=True) alpha_list[i][j] = np.sqrt( np.sum( np.abs( np.ndarray.flatten(meas_coeff_err[:N_]) - np.sqrt(meas_coeff))**2)) m_state = meas_coeff_err system = mixed_state(max_coh, s) val_list[i][j] = moment(n) / moment(1)**( n - 1) #-func(max_coh+meas_coeff_err,s,n) for i in range(N_err): #plt.plot(epsilon_list,val_list[i]) plt.plot(alpha_list[i], val_list[i]) plt.plot([0., np.max(alpha_list)], [low_bound, low_bound], 'k--') plt.xlim(0, np.max(alpha_list)) plt.xlabel('||U|x>-|x>||', fontsize=16) plt.ylabel(r'$F_3$', fontsize=16, rotate=90)
def test_adjoint_of_entropy_exchange(): r"""Test the adjoint of the entropy exchange on pauli channel example.""" p1, p2, p3 = 0.1, 0.01, 0.4 krauss_ops = initialize_pauli_examples(p1, p2, p3) dense_krauss_obj = DenseKraus(krauss_ops, [1, 1], 2, 2) sparse_krauss_obj = SparseKraus(krauss_ops, [1, 1], 2, 2) # Test on random density matrices for n in [1, 2]: if n != 1: krauss_ops = np.kron(krauss_ops, krauss_ops) for _ in range(0, 50): dense_krauss_obj.update_kraus_operators(n) sparse_krauss_obj.update_kraus_operators(n) rho = np.array(rand_dm_ginibre(4**n).data.todense()) desired = np.zeros((2**n, 2**n), dtype=np.complex128) for i, k1 in enumerate(krauss_ops): for j, k2 in enumerate(krauss_ops): desired += np.conj(k2.T).dot(k1) * rho[j, i] dense_chan = dense_krauss_obj.entropy_exchange(rho, n, True) sparse_chan = sparse_krauss_obj.entropy_exchange(rho, n, True).todense() assert_array_almost_equal(dense_chan, sparse_chan) assert_array_almost_equal(desired, dense_chan) assert_array_almost_equal(desired, sparse_chan) # Test that adjoint maps of compl. positive maps are always unital rho = np.eye(4**n) desired = np.eye(2**n) assert_array_almost_equal( desired, sparse_krauss_obj.entropy_exchange(rho, n, True).todense()) assert_array_almost_equal(desired, dense_krauss_obj.entropy_exchange(rho, n, True))
def _sample(self): # Generate and flatten a density operator, so that we can multiply it # by the transformation defined above. rho = qt.rand_dm_ginibre(self._dim, self._rank).data.todense().view(np.ndarray).flatten(order='C') return np.real(np.dot(self._paulis, rho))
def _sample_dm(self): # Generate and flatten a density operator, so that we can multiply it # by the transformation defined above. return qt.rand_dm_ginibre(self._dim, rank=self._rank)
def _sample(self): # Generate and flatten a density operator, so that we can multiply it # by the transformation defined above. rho = qt.rand_dm_ginibre(self._dim, self._rank).data.todense().view( np.ndarray).flatten(order='C') return np.real(np.dot(self._paulis, rho))
def test_standard_basis(): r"""Tests for "PyQCodes.param.StandardBasis" class.""" # Test going from vectors to density states. numb_tests = 1000 for n in range(1, 3): for _ in range(0, numb_tests): v = np.random.uniform(-1., 1., 2**(n * 2)) actual_rho = StandardBasis.rho_from_vec(v, 2**n) if n == 1: desired_rho = np.array( [[complex(v[0], 0.), complex(v[1], v[2])], [complex(v[1], -v[2]), complex(v[3], 0.)]]) elif n == 2: desired_rho = np.array([[ complex(v[0], 0.), complex(v[1], v[2]), complex(v[3], v[4]), complex(v[5], v[6]) ], [ complex(v[1], -v[2]), complex(v[7], 0.), complex(v[8], v[9]), complex(v[10], v[11]) ], [ complex(v[3], -v[4]), complex(v[8], -v[9]), complex(v[12], 0.), complex(v[13], v[14]) ], [ complex(v[5], -v[6]), complex(v[10], -v[11]), complex(v[13], -v[14]), complex(v[15], 0.) ]]) assert_array_almost_equal(actual_rho, desired_rho) # Test going from rho to vector to rho. for n in range(1, 10): rho = rand_dm_ginibre(2**n).data.todense() vec = StandardBasis.vec_from_rho(rho, 2**n) actual_rho = StandardBasis.rho_from_vec(vec, 2**n) assert_array_almost_equal(actual_rho, rho) # Test going from rho to vector. rho = np.array([[1., 1 + 2.j], [1 - 2.j, 3.]]) desired_vec = np.array([1., 1., 2., 3.]) actual_vec = StandardBasis.vec_from_rho(rho, 2) assert_array_almost_equal(actual_vec, desired_vec) # Test different dimension rho = np.array([[0.25, 0.2 + 0.1j, 0.3], [0.5 - 0.1j, 0.25, 0.23 + 0.1j], [0.3, 0.23 - 0.1j, 0.5]]) dim = 3 vec = StandardBasis.vec_from_rho(rho, dim) desired = [0.25, 0.2, 0.1, 0.3, 0, 0.25, 0.23, 0.1, 0.5] assert_array_almost_equal(desired, vec) assert StandardBasis.numb_variables(dim) == dim**2