def pr_box(eta=1, name=False): """ The Popescu-Rohrlich box, or PR box, is the canonical non-signalling, non-local probability distribution used in the study of superquantum correlations. It has two space-like seperated inputs, X and Y, and two associated outputs, A and B. `eta` is the noise level of this correlation. For 0 <= eta <= 1/2 the box can be realized classically. For 1/2 < eta <= 1/sqrt(2) the box can be realized quantum-mechanically. Parameters ---------- eta : float, 0 <= eta <= 1 The noise level of the box. Defaults to 1. name : bool Whether to set rv names or not. Defaults to False. Returns ------- pr : Distribution The PR box distribution. """ outcomes = list(product([0, 1], repeat=4)) pmf = [ ((1+eta)/16 if (x*y == a^b) else (1-eta)/16) for x, y, a, b in outcomes ] pr = Distribution(outcomes, pmf) if name: pr.set_rv_names("XYAB") return pr
def test_parse_rvs2(): outcomes = ['00', '11'] pmf = [1 / 2] * 2 d = Distribution(outcomes, pmf) d.set_rv_names('XY') with pytest.raises(ditException): parse_rvs(d, ['X', 'Y', 'Z'])
def test_parse_rvs2(): outcomes = ['00', '11'] pmf = [1/2]*2 d = Distribution(outcomes, pmf) d.set_rv_names('XY') with pytest.raises(ditException): parse_rvs(d, ['X', 'Y', 'Z'])
def pr_box(eta=1, name=False): """ The Popescu-Rohrlich box, or PR box, is the canonical non-signalling, non-local probability distribution used in the study of superquantum correlations. It has two space-like seperated inputs, X and Y, and two associated outputs, A and B. `eta` is the noise level of this correlation. For 0 <= eta <= 1/2 the box can be realized classically. For 1/2 < eta <= 1/sqrt(2) the box can be realized quantum-mechanically. Parameters ---------- eta : float, 0 <= eta <= 1 The noise level of the box. Defaults to 1. name : bool Whether to set rv names or not. Defaults to False. Returns ------- pr : Distribution The PR box distribution. """ outcomes = list(product([0, 1], repeat=4)) pmf = [((1 + eta) / 16 if (x * y == a ^ b) else (1 - eta) / 16) for x, y, a, b in outcomes] pr = Distribution(outcomes, pmf) if name: pr.set_rv_names("XYAB") return pr
def test_K4(): outcomes = ['00', '01', '10', '11', '22', '33'] pmf = [1/8, 1/8, 1/8, 1/8, 1/4, 1/4] d = Distribution(outcomes, pmf) assert_almost_equal(K(d), 1.5) assert_almost_equal(K(d, [[0],[1]]), 1.5) d.set_rv_names("XY") assert_almost_equal(K(d, [['X'],['Y']]), 1.5)
def test_K1(): outcomes = ['00', '11'] pmf = [1/2, 1/2] d = Distribution(outcomes, pmf) assert_almost_equal(K(d), 1.0) assert_almost_equal(K(d, [[0],[1]]), 1.0) d.set_rv_names("XY") assert_almost_equal(K(d, [['X'],['Y']]), 1.0)
def test_K3(): """ Test K for mixed events """ outcomes = ['00', '01', '11'] pmf = [1/3, 1/3, 1/3] d = Distribution(outcomes, pmf) assert_almost_equal(K(d), 0.0) assert_almost_equal(K(d, [[0], [1]]), 0.0) d.set_rv_names("XY") assert_almost_equal(K(d, [['X'], ['Y']]), 0.0)
def test_fci2(): """ Test known values w/ rv names. """ d = Distribution(['000', '011', '101', '110'], [1 / 4] * 4) d.set_rv_names('XYZ') assert F(d) == pytest.approx(2.0) assert F(d, ['X', 'Y']) == pytest.approx(0.0) assert F(d, ['X', 'Y'], 'Z') == pytest.approx(1.0)
def test_K3(): """ Test K for mixed events """ outcomes = ['00', '01', '11'] pmf = [1 / 3, 1 / 3, 1 / 3] d = Distribution(outcomes, pmf) assert_almost_equal(K(d), 0.0) assert_almost_equal(K(d, [[0], [1]]), 0.0) d.set_rv_names("XY") assert_almost_equal(K(d, [['X'], ['Y']]), 0.0)
def test_K4(): """ Test K in a canonical example """ outcomes = ['00', '01', '10', '11', '22', '33'] pmf = [1/8, 1/8, 1/8, 1/8, 1/4, 1/4] d = Distribution(outcomes, pmf) assert K(d) == pytest.approx(1.5) assert K(d, [[0], [1]]) == pytest.approx(1.5) d.set_rv_names("XY") assert K(d, [['X'], ['Y']]) == pytest.approx(1.5)
def test_K3(): """ Test K for mixed events """ outcomes = ['00', '01', '11'] pmf = [1/3, 1/3, 1/3] d = Distribution(outcomes, pmf) assert K(d) == pytest.approx(0.0) assert K(d, [[0], [1]]) == pytest.approx(0.0) d.set_rv_names("XY") assert K(d, [['X'], ['Y']]) == pytest.approx(0.0)
def test_K1(): """ Test K for dependent events """ outcomes = ['00', '11'] pmf = [1 / 2] * 2 d = Distribution(outcomes, pmf) assert K(d) == pytest.approx(1.0) assert K(d, [[0], [1]]) == pytest.approx(1.0) d.set_rv_names("XY") assert K(d, [['X'], ['Y']]) == pytest.approx(1.0)
def test_K3(): """ Test K for mixed events """ outcomes = ['00', '01', '11'] pmf = [1 / 3] * 3 d = Distribution(outcomes, pmf) assert K(d) == pytest.approx(0.0) assert K(d, [[0], [1]]) == pytest.approx(0.0) d.set_rv_names("XY") assert K(d, [['X'], ['Y']]) == pytest.approx(0.0)
def test_K1(): """ Test K for dependent events """ outcomes = ['00', '11'] pmf = [1/2, 1/2] d = Distribution(outcomes, pmf) assert K(d) == pytest.approx(1.0) assert K(d, [[0], [1]]) == pytest.approx(1.0) d.set_rv_names("XY") assert K(d, [['X'], ['Y']]) == pytest.approx(1.0)
def test_K4(): """ Test K in a canonical example """ outcomes = ['00', '01', '10', '11', '22', '33'] pmf = [1 / 8, 1 / 8, 1 / 8, 1 / 8, 1 / 4, 1 / 4] d = Distribution(outcomes, pmf) assert K(d) == pytest.approx(1.5) assert K(d, [[0], [1]]) == pytest.approx(1.5) d.set_rv_names("XY") assert K(d, [['X'], ['Y']]) == pytest.approx(1.5)
def test_K1(): """ Test K for dependent events """ outcomes = ['00', '11'] pmf = [1 / 2, 1 / 2] d = Distribution(outcomes, pmf) assert_almost_equal(K(d), 1.0) assert_almost_equal(K(d, [[0], [1]]), 1.0) d.set_rv_names("XY") assert_almost_equal(K(d, [['X'], ['Y']]), 1.0)
def test_K2(): """ Test conditional K for dependent events """ outcomes = ['00', '11'] pmf = [1 / 2, 1 / 2] d = Distribution(outcomes, pmf) assert K(d, [[0], [1]], [0]) == pytest.approx(0.0) assert K(d, [[0], [1]], [1]) == pytest.approx(0.0) d.set_rv_names("XY") assert K(d, [['X'], ['Y']], ['X']) == pytest.approx(0.0) assert K(d, [['X'], ['Y']], ['Y']) == pytest.approx(0.0)
def test_K2(): """ Test conditional K for dependent events """ outcomes = ['00', '11'] pmf = [1/2, 1/2] d = Distribution(outcomes, pmf) assert_almost_equal(K(d, [[0], [1]], [0]), 0.0) assert_almost_equal(K(d, [[0], [1]], [1]), 0.0) d.set_rv_names("XY") assert_almost_equal(K(d, [['X'], ['Y']], ['X']), 0.0) assert_almost_equal(K(d, [['X'], ['Y']], ['Y']), 0.0)
def test_K5(): outcomes = ['000', '010', '100', '110', '221', '331'] pmf = [1/8, 1/8, 1/8, 1/8, 1/4, 1/4] d = Distribution(outcomes, pmf) assert_almost_equal(K(d, [[0],[1]]), 1.5) assert_almost_equal(K(d), 1.0) assert_almost_equal(K(d, [[0],[1],[2]]), 1.0) d.set_rv_names("XYZ") assert_almost_equal(K(d, [['X'],['Y']]), 1.5) assert_almost_equal(K(d, [['X'],['Y'],['Z']]), 1.0) assert_almost_equal(K(d, ['X', 'Y'], ['Z']), 0.5) assert_almost_equal(K(d, ['XY', 'YZ']), 2.0)
def test_K5(): """ Test K on subvariables and conditionals """ outcomes = ['000', '010', '100', '110', '221', '331'] pmf = [1 / 8, 1 / 8, 1 / 8, 1 / 8, 1 / 4, 1 / 4] d = Distribution(outcomes, pmf) assert K(d, [[0], [1]]) == pytest.approx(1.5) assert K(d) == pytest.approx(1.0) assert K(d, [[0], [1], [2]]) == pytest.approx(1.0) d.set_rv_names("XYZ") assert K(d, [['X'], ['Y']]) == pytest.approx(1.5) assert K(d, [['X'], ['Y'], ['Z']]) == pytest.approx(1.0) assert K(d, ['X', 'Y'], ['Z']) == pytest.approx(0.5) assert K(d, ['XY', 'YZ']) == pytest.approx(2.0)
def test_K5(): """ Test K on subvariables and conditionals """ outcomes = ['000', '010', '100', '110', '221', '331'] pmf = [1/8, 1/8, 1/8, 1/8, 1/4, 1/4] d = Distribution(outcomes, pmf) assert K(d, [[0], [1]]) == pytest.approx(1.5) assert K(d) == pytest.approx(1.0) assert K(d, [[0], [1], [2]]) == pytest.approx(1.0) d.set_rv_names("XYZ") assert K(d, [['X'], ['Y']]) == pytest.approx(1.5) assert K(d, [['X'], ['Y'], ['Z']]) == pytest.approx(1.0) assert K(d, ['X', 'Y'], ['Z']) == pytest.approx(0.5) assert K(d, ['XY', 'YZ']) == pytest.approx(2.0)
def test_M2(): """ Test M with rv names """ outcomes = ['000', 'a00', '00c', 'a0c', '011', 'a11', '101', 'b01', '01d', 'a1d', '10d', 'b0d', '110', 'b10', '11c', 'b1c',] pmf = [1/16]*16 d = Distribution(outcomes, pmf) d.set_rv_names('XYZ') assert_almost_equal(M(d), 2.0)
def test_M2(): """ Test M with rv names """ outcomes = [ '000', 'a00', '00c', 'a0c', '011', 'a11', '101', 'b01', '01d', 'a1d', '10d', 'b0d', '110', 'b10', '11c', 'b1c', ] pmf = [1 / 16] * 16 d = Distribution(outcomes, pmf) d.set_rv_names('XYZ') assert M(d) == pytest.approx(2.0)
from hypothesis import given, settings import numpy as np from dit import Distribution from dit.example_dists.intrinsic import intrinsic_1, intrinsic_2, intrinsic_3 from dit.exceptions import ditException from dit.multivariate import total_correlation from dit.multivariate.secret_key_agreement import intrinsic_mutual_informations as IMI from dit.utils.testing import distributions dist1 = Distribution([(0,0,0), (0,1,1), (1,0,1), (1,1,0), (2,2,2), (3,3,3)], [1/8]*4+[1/4]*2) dist2 = Distribution(['000', '011', '101', '110', '222', '333'], [1/8]*4+[1/4]*2) dist2.set_rv_names('XYZ') dist3 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2) dist4 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2) dist4.set_rv_names('VWXYZ') dist5 = Distribution(['0000', '0011', '0101', '0110', '1001', '1010', '1100', '1111'], [1/8]*8) dist6 = Distribution(['0000', '0011', '0101', '0110', '1001', '1010', '1100', '1111'], [1/8]*8) dist6.set_rv_names('WXYZ') @pytest.mark.flaky(reruns=5) def test_itc1(): """ Test against standard result. """ itc = IMI.intrinsic_total_correlation(dist1, [[0], [1]], [2]) assert itc == pytest.approx(0)
""" from __future__ import division import pytest import numpy as np from dit import Distribution from dit.profiles import SchneidmanProfile ex1 = Distribution(['000', '001', '010', '011', '100', '101', '110', '111'], [1/8]*8) ex2 = Distribution(['000', '111'], [1/2]*2) ex3 = Distribution(['000', '001', '110', '111'], [1/4]*4) ex4 = Distribution(['000', '011', '101', '110'], [1/4]*4) ex4.set_rv_names('XYZ') @pytest.mark.parametrize(('ex', 'prof'), [ (ex1, (0.0, 0.0, 0.0)), (ex2, (0.0, 2.0, 0.0)), (ex3, (0.0, 1.0, 0.0)), (ex4, (0.0, 0.0, 1.0)), ]) def test_schneidman_profile(ex, prof): """ Test against known examples. """ sp = SchneidmanProfile(ex) assert np.allclose([sp.profile[i] for i in (1, 2, 3)], prof, atol=1e-5)
def test_parse_rvs2(): outcomes = ['00', '11'] pmf = [1/2]*2 d = Distribution(outcomes, pmf) d.set_rv_names('XY') assert_raises(ditException, parse_rvs, d, ['X', 'Y', 'Z'])
def test_parse_rvs2(): outcomes = ['00', '11'] pmf = [1 / 2] * 2 d = Distribution(outcomes, pmf) d.set_rv_names('XY') assert_raises(ditException, parse_rvs, d, ['X', 'Y', 'Z'])
""" from __future__ import division import pytest import numpy as np from dit import Distribution from dit.profiles import ConnectedInformations, ConnectedDualInformations ex1 = Distribution(['000', '001', '010', '011', '100', '101', '110', '111'], [1/8]*8) ex2 = Distribution(['000', '111'], [1/2]*2) ex3 = Distribution(['000', '001', '110', '111'], [1/4]*4) ex4 = Distribution(['000', '011', '101', '110'], [1/4]*4) ex4.set_rv_names('XYZ') @pytest.mark.parametrize(('ex', 'prof'), [ (ex1, (0.0, 0.0, 0.0)), (ex2, (0.0, 2.0, 0.0)), (ex3, (0.0, 1.0, 0.0)), (ex4, (0.0, 0.0, 1.0)), ]) def test_connected_information(ex, prof): """ Test against known examples. """ ci = ConnectedInformations(ex) assert np.allclose([ci.profile[i] for i in (1, 2, 3)], prof, atol=1e-5)
from hypothesis import given import numpy as np from dit import Distribution from dit.example_dists.intrinsic import intrinsic_1, intrinsic_2, intrinsic_3 from dit.exceptions import ditException from dit.multivariate import total_correlation from dit.multivariate.secret_key_agreement import intrinsic_mutual_informations as IMI from dit.utils.testing import distributions dist1 = Distribution([(0,0,0), (0,1,1), (1,0,1), (1,1,0), (2,2,2), (3,3,3)], [1/8]*4+[1/4]*2) dist2 = Distribution(['000', '011', '101', '110', '222', '333'], [1/8]*4+[1/4]*2) dist2.set_rv_names('XYZ') dist3 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2) dist4 = Distribution(['00000', '00101', '11001', '11100', '22220', '33330'], [1/8]*4+[1/4]*2) dist4.set_rv_names('VWXYZ') dist5 = Distribution(['0000', '0011', '0101', '0110', '1001', '1010', '1100', '1111'], [1/8]*8) dist6 = Distribution(['0000', '0011', '0101', '0110', '1001', '1010', '1100', '1111'], [1/8]*8) dist6.set_rv_names('WXYZ') @pytest.mark.flaky(reruns=5) def test_itc1(): """ Test against standard result. """ itc = IMI.intrinsic_total_correlation(dist1, [[0], [1]], [2]) assert itc == pytest.approx(0)