def test_density_angle() -> None: rho0 = qf.random_density(1) rho1 = qf.random_density(1) qf.density_angle(rho0, rho1) assert not qf.densities_close(rho0, rho1) assert qf.densities_close(rho0, rho0)
def test_fidelity() -> None: rho0 = qf.random_density(4) rho1 = qf.random_density(4) fid = qf.fidelity(rho0, rho1) assert 0.0 <= fid <= 1.0 rho2 = qf.random_density([3, 2, 1, 0]) fid = qf.fidelity(rho0, rho2) assert 0.0 <= fid <= 1.0 fid = qf.fidelity(rho0, rho0) assert np.isclose(fid, 1.0) ket0 = qf.random_state(3) ket1 = qf.random_state(3) fid0 = qf.state_fidelity(ket0, ket1) rho0 = ket0.asdensity() rho1 = ket1.asdensity() fid1 = qf.fidelity(rho0, rho1) assert np.isclose(fid1, fid0) fid2 = np.cos(qf.fubini_study_angle(ket0.tensor, ket1.tensor)) ** 2 assert np.isclose(fid2, fid0)
def test_bures_distance(repeat: int) -> None: rho = qf.random_density(4) # Note ATOL. Sometimes does not give accurate answer assert np.isclose(qf.bures_distance(rho, rho), 0.0, atol=ATOL * 100) rho1 = qf.random_density(4) qf.bures_distance(rho, rho1)
def test_fidelity(): rho0 = qf.random_density(4) rho1 = qf.random_density(4) fid = qf.fidelity(rho0, rho1) print('FID', fid) assert 0.0 <= fid <= 1.0 rho2 = qf.random_density([3, 2, 1, 0]) fid = qf.fidelity(rho0, rho2) assert 0.0 <= fid <= 1.0 fid = qf.fidelity(rho0, rho0) print('FID', fid) assert fid == ALMOST_ONE ket0 = qf.random_state(3) ket1 = qf.random_state(3) fid0 = qf.state_fidelity(ket0, ket1) rho0 = ket0.asdensity() rho1 = ket1.asdensity() fid1 = qf.fidelity(rho0, rho1) assert qf.asarray(fid1 - fid0) == ALMOST_ZERO fid2 = bk.cos(qf.fubini_study_angle(ket0.vec, ket1.vec))**2 assert qf.asarray(fid2 - fid0) == ALMOST_ZERO
def test_random_density() -> None: rho = qf.random_density(4) assert list(rho.tensor.shape) == [2] * 8 rho1 = qf.random_density(4, rank=2, ensemble="Hilbert–Schmidt") assert list(rho1.tensor.shape) == [2] * 8 rho2 = qf.random_density(4, rank=2, ensemble="Bures") assert list(rho2.tensor.shape) == [2] * 8 with pytest.raises(ValueError): qf.random_density(4, rank=2, ensemble="not_an_ensemble")
def test_identity(): chan = qf.identity_gate(1).aschannel() rho = qf.random_density(2) after = chan.evolve(rho) assert qf.densities_close(rho, after) assert chan.name == 'Channel'
def test_bures_angle(): rho = qf.random_density(4) assert qf.bures_angle(rho, rho) == ALMOST_ZERO rho1 = qf.random_density(4) qf.bures_angle(rho, rho1) ket0 = qf.random_state(4) ket1 = qf.random_state(4) rho0 = ket0.asdensity() rho1 = ket1.asdensity() ang0 = qf.fubini_study_angle(ket0.vec, ket1.vec) ang1 = qf.bures_angle(rho0, rho1) assert np.isclose(ang0, ang1)
def test_depolarizing() -> None: p = 1.0 - np.exp(-1 / 20) chan = qf.Depolarizing(p, 0).aschannel() rho0 = qf.Density([[0.5, 0], [0, 0.5]]) assert qf.densities_close(rho0, chan.evolve(rho0)) rho0 = qf.random_density(1) rho1 = chan.evolve(rho0) pr0 = np.real(qf.purity(rho0)) pr1 = np.real(qf.purity(rho1)) assert pr0 > pr1 # Test data extracted from refereneqvm rho2 = qf.Density([[0.43328691, 0.48979689], [0.48979689, 0.56671309]]) rho_test = qf.Density( [[0.43762509 + 0.0j, 0.45794666 + 0.0j], [0.45794666 + 0.0j, 0.56237491 + 0.0j]] ) assert qf.densities_close(chan.evolve(rho2), rho_test) ket0 = qf.random_state(1) qf.Depolarizing(p, 0).run(ket0) rho1b = qf.Depolarizing(p, 0).evolve(rho0) assert qf.densities_close(rho1, rho1b)
def test_identity() -> None: chan = qf.IdentityGate([1]).aschannel() rho = qf.random_density(2) after = chan.evolve(rho) assert qf.densities_close(rho, after) assert chan.name == "Channel"
def test_bures_angle() -> None: rho = qf.random_density(4) assert np.isclose(qf.bures_angle(rho, rho), 0.0, atol=ATOL * 10) rho1 = qf.random_density(4) qf.bures_angle(rho, rho1) ket0 = qf.random_state(4) ket1 = qf.random_state(4) rho0 = ket0.asdensity() rho1 = ket1.asdensity() ang0 = qf.fubini_study_angle(ket0.tensor, ket1.tensor) ang1 = qf.bures_angle(rho0, rho1) assert np.isclose(ang0, ang1)
def test_purity(): density = qf.ghz_state(4).asdensity() assert qf.asarray(qf.purity(density)) == ALMOST_ONE for _ in range(10): rho = qf.random_density(4) purity = np.real(qf.asarray(qf.purity(rho))) assert purity < 1.0 assert purity >= 0.0
def test_density_trace(): rho = qf.random_density(3) assert qf.asarray(rho.trace()) == ALMOST_ONE rho = qf.Density(np.eye(8)) assert np.isclose(qf.asarray(rho.trace()), 8) rho = rho.normalize() assert np.isclose(qf.asarray(rho.trace()), 1)
def test_density_trace() -> None: rho = qf.random_density(3) assert np.isclose(rho.trace(), 1) rho = qf.Density(np.eye(8)) assert np.isclose(rho.trace(), 8) rho = rho.normalize() assert np.isclose(rho.trace(), 1)
def test_purity() -> None: density = qf.ghz_state(4).asdensity() assert np.isclose(qf.purity(density), 1.0) for _ in range(10): rho = qf.random_density(4) purity = np.real(qf.purity(rho)) assert purity < 1.0 assert purity >= 0.0
def test_gate_evolve(gatet: Type[qf.StdGate]) -> None: gate0 = _randomize_gate(gatet) gate1 = qf.Unitary(gate0.tensor, gate0.qubits) rho = qf.random_density(gate0.qubits) rho0 = gate0.evolve(rho) rho1 = gate1.evolve(rho) assert qf.densities_close(rho0, rho1)
def test_random_channel() -> None: chan = qf.random_channel([0, 1, 2], rank=4) rho0 = qf.random_density([0, 1, 2], rank=2) purity0 = qf.purity(rho0) rho1 = chan.evolve(rho0) purity1 = qf.purity(rho1) assert purity1 < purity0 chan = qf.random_channel([0, 1, 2], rank=4, unital=True) assert qf.almost_unital(chan)
def test_purity() -> None: density = qf.ghz_state(4).asdensity() assert np.isclose(qf.purity(density), 1.0) for _ in range(10): density = qf.random_density(4) purity = np.real(qf.purity(density)) assert purity < 1.0 assert purity >= 0.0 rho = qf.Density(np.diag([0.9, 0.1])) assert np.isclose(qf.purity(rho), 0.82) # Kudos: Josh Combs
def test_purity(): density = qf.ghz_state(4).asdensity() assert qf.asarray(qf.purity(density)) == ALMOST_ONE for _ in range(10): density = qf.random_density(4) purity = np.real(qf.asarray(qf.purity(density))) assert purity < 1.0 assert purity >= 0.0 rho = qf.Density(np.diag([0.9, 0.1])) assert np.isclose(qf.asarray(qf.purity(rho)), 0.82) # Kudos: Josh Combes
def test_reset(): reset = qf.Reset(0, 1, 2) with pytest.raises(TypeError): reset.evolve(qf.random_density([0, 1, 3])) with pytest.raises(TypeError): reset.asgate() with pytest.raises(TypeError): reset.aschannel() assert reset.H is reset
def test_reset() -> None: reset = qf.Reset(0, 1, 2) ket = qf.random_state([0, 1, 2, 3]) ket = reset.run(ket) assert ket.tensor[1, 1, 0, 0] == 0.0 assert str(reset) == "Reset 0 1 2" reset = qf.Reset() ket = qf.random_state([0, 1, 2, 3]) ket = reset.run(ket) assert qf.states_close(ket, qf.zero_state([0, 1, 2, 3])) assert str(reset) == "Reset" with pytest.raises(TypeError): reset.evolve(qf.random_density([0, 1, 3])) with pytest.raises(TypeError): reset.asgate() with pytest.raises(TypeError): reset.aschannel()
def test_transpose_map() -> None: # The transpose map is a superoperator that transposes a 1-qubit # density matrix. Not physical. # quant-ph/0202124 q0 = 0 ops = [ qf.Unitary(np.asarray([[1, 0], [0, 0]]), [q0]), qf.Unitary(np.asarray([[0, 0], [0, 1]]), [q0]), qf.Unitary(np.asarray([[0, 1], [1, 0]]) / np.sqrt(2), [q0]), qf.Unitary(np.asarray([[0, 1], [-1, 0]]) / np.sqrt(2), [q0]), ] kraus = qf.Kraus(ops, weights=(1, 1, 1, -1)) rho0 = qf.random_density(1) rho1 = kraus.evolve(rho0) op0 = rho0.asoperator() op1 = rho1.asoperator() assert np.allclose(op0.T, op1) # The Choi matrix should be same as Swap operator choi = kraus.aschannel().choi() assert np.allclose(choi, qf.Swap(0, 2).asoperator())
def test_transpose_map(): # The transpose map is a superoperator that transposes a 1-qubit # density matrix. Not physical. # quant-ph/0202124 ops = [ qf.Gate(np.asarray([[1, 0], [0, 0]])), qf.Gate(np.asarray([[0, 0], [0, 1]])), qf.Gate(np.asarray([[0, 1], [1, 0]]) / np.sqrt(2)), qf.Gate(np.asarray([[0, 1], [-1, 0]]) / np.sqrt(2)) ] kraus = qf.Kraus(ops, weights=(1, 1, 1, -1)) rho0 = qf.random_density(1) rho1 = kraus.evolve(rho0) op0 = qf.asarray(rho0.asoperator()) op1 = qf.asarray(rho1.asoperator()) assert np.allclose(op0.T, op1) # The Choi matrix should be same as SWAP operator choi = kraus.aschannel().choi() choi = qf.asarray(choi) assert np.allclose(choi, qf.asarray(qf.SWAP(0, 2).asoperator()))
def test_bures_distance(): rho = qf.random_density(4) assert qf.bures_distance(rho, rho) == ALMOST_ZERO rho1 = qf.random_density(4) qf.bures_distance(rho, rho1)
def test_trace_distance() -> None: rho = qf.random_density(4) assert np.isclose(qf.trace_distance(rho, rho), 0.0) rho1 = qf.random_density(4) qf.trace_distance(rho, rho1)
def test_random_density(): rho = qf.random_density(4) assert list(rho.vec.asarray().shape) == [2] * 8
def test_random_density() -> None: rho = qf.random_density(4) assert rho.tensor.shape == (2,) * 8