Example #1
0
    def main():
        num_of_comps = 1

        tau_mag = .5
        tau_degs = 35
        rho_degs = 45

        if num_of_comps == 1:
            size1 = 3
            size2 = 4
            max_n_sum = 5
            pa1 = BayesNode(0, "parent1", size=size1)
            pa2 = BayesNode(1, "parent2", size=size2)

            pa1.state_names = [str(k) for k in range(size1)]
            pa2.state_names = [str(k) for k in range(size2)]

            bs = BeamSplitter(2, "a_bs", pa1, pa2,
                    tau_mag, tau_degs, rho_degs, num_of_comps, max_n_sum)

            print("pa1 state names: ", pa1.state_names)
            print("pa2 state names: ", pa2.state_names)
            print("bs state names: ", bs.state_names)
            print(bs.potential)
            print("full dict of total probs: ",
                  bs.potential.get_total_probs())
            print("brief dict of total probs: ",
                  bs.potential.get_total_probs(brief=True))
        elif num_of_comps == 2:
            size1 = 6
            size2 = 8
            max_n_sum = 7
            pa1 = BayesNode(0, "parent1", size=size1)
            pa2 = BayesNode(1, "parent2", size=size2)

            pa1.set_state_names_to_product(
                [range(2), range(3)], trim=False)
            pa2.set_state_names_to_product(
                [range(2), range(4)], trim=False)

            bs = BeamSplitter(2, "a_bs", pa1, pa2,
                    tau_mag, tau_degs, rho_degs, num_of_comps, max_n_sum)

            print("pa1 state names: ", pa1.state_names)
            print("pa2 state names: ", pa2.state_names)
            print("bs state names: ", bs.state_names)
            print(bs.potential)
            print("full dict of total probs: ",
                  bs.potential.get_total_probs())
            print("brief dict of total probs: ",
                  bs.potential.get_total_probs(brief=True))
Example #2
0
    def main():
        pa1 = BayesNode(0, "parent1", size=2)
        pa2 = BayesNode(1, "parent2", size=2)

        pa1.state_names = ['0', '1']
        pa2.state_names = ['0', '1']

        cn = CNot(2, "a_cnot", False, pa1, pa2, True, True)

        print("pa1 state names: ", pa1.state_names)
        print("pa2 state names: ", pa2.state_names)
        print("cnot state names: ", cn.state_names)
        print(cn.potential)
        print(cn.potential.get_total_probs())
    def main():
        pa_nd = BayesNode(0, "pa_nd", size=4)
        pa_nd.state_names = [str(k) for k in range(4)]

        p_sh = PhaseShifter(1, "pshifter", pa_nd, 30, occ_nums=True)

        print("pa_nd state names: ", pa_nd.state_names)
        print("phase shifter state names: ", p_sh.state_names)
        print(p_sh.potential)
        print(p_sh.potential.get_total_probs())
Example #4
0
    def main():
        pa_nd = BayesNode(0, "pa_nd", size=2)
        pa_nd.state_names = ['0', '1']

        thetas_degs = [20, 30, 40, 60]
        qr = QubitRot(1, "rot", pa_nd, thetas_degs)

        print("pa_nd state names: ", pa_nd.state_names)
        print("qubit rot state names: ", qr.state_names)
        print(qr.potential)
        print(qr.potential.get_total_probs())
Example #5
0
    def read_bif(path, is_quantum):
        """
        Reads a bif file using our stand-alone class BifTool and returns a
        BayesNet. bif and dot files complement each other. bif: graphical
        info No, pot info Yes. dot: graphical info Yes, pot info No. By pots
        I mean potentials, the transition matrices of the nodes. (aka CPTs,
        etc.)

        Parameters
        ----------
        path : str
        is_quantum : bool

        Returns
        -------
        BayesNet

        """

        bt = BifTool(is_quantum)
        bt.read_bif(path)
        nodes = set()
        name_to_nd = {}
        for k, nd_name in enumerate(bt.nd_sizes.keys()):
            node = BayesNode(k, nd_name)
            node.state_names = bt.states[nd_name]
            node.size = len(node.state_names)
            node.forget_all_evidence()
            nodes |= {node}
            name_to_nd[nd_name] = node
        for nd_name, pa_name_list in bt.parents.items():
            node = name_to_nd[nd_name]
            for pa_name in pa_name_list:
                pa = name_to_nd[pa_name]
                node.add_parent(pa)

        for nd_name, parent_names in bt.parents.items():
            node = name_to_nd[nd_name]
            num_pa = len(parent_names)
            parents = [name_to_nd[pa_name] for pa_name in parent_names]
            if num_pa == 0:
                node.potential = DiscreteUniPot(is_quantum, node)
            else:
                node.potential = DiscreteCondPot(
                    is_quantum, parents + [node])
            node.potential.pot_arr = bt.pot_arrays[nd_name]

        return BayesNet(nodes)
Example #6
0
        elif n == 1 and m == 1:
            x = c
            y = -theta_hat[3] * s
        elif n == 0 and m == 1:
            x = theta_hat[2] * s
            y = theta_hat[1] * s
        elif n == 1 and m == 0:
            x = -theta_hat[2] * s
            y = theta_hat[1] * s
        else:
            x = 0
            y = 0

        s = math.sin(rads[0])
        c = math.cos(rads[0])

        return (c * x - s * y) + 1j * (c * y + s * x)


if __name__ == "__main__":
    pa_nd = BayesNode(0, "pa_nd", size=2)
    pa_nd.state_names = ['0', '1']

    thetas_degs = [20, 30, 40, 60]
    qr = QubitRot(1, "rot", pa_nd, thetas_degs)

    print("pa_nd state names: ", pa_nd.state_names)
    print("qubit rot state names: ", qr.state_names)
    print(qr.potential)
    print(qr.potential.get_total_probs())
Example #7
0
        self.occ_nums = occ_nums

        BayesNode.__init__(self, id_num, name, size=pa_nd.size)
        self.add_parent(pa_nd)

        self.state_names = pa_nd.state_names

        pot = DiscreteCondPot(True, [pa_nd, self], bias=0)
        self.potential = pot

        theta_rads = theta_degs * math.pi / 180
        for k in range(pa_nd.size):
            if not occ_nums:
                phase = theta_rads
            else:
                num = int(pa_nd.state_names[k])
                phase = num * theta_rads
            self.potential[k, k] = cmath.exp(1j * phase)


if __name__ == "__main__":
    pa_nd = BayesNode(0, "pa_nd", size=4)
    pa_nd.state_names = [str(k) for k in range(4)]

    p_sh = PhaseShifter(1, "pshifter", pa_nd, 30, occ_nums=True)

    print("pa_nd state names: ", pa_nd.state_names)
    print("phase shifter state names: ", p_sh.state_names)
    print(p_sh.potential)
    print(p_sh.potential.get_total_probs())
Example #8
0
        """
        if self.flipped_by_0:
            x = 1 - pa1_st
        else:  # flipped by 1
            x = pa1_st
        if self.pa1_is_control:
            bit0 = pa1_st
            bit1 = (x + pa2_st) // 2
        else:  # pa2 is control
            bit0 = (x + pa2_st) // 2
            bit1 = pa2_st
        foc_st = bit0 + 2 * bit1
        return foc_st


if __name__ == "__main__":
    pa1 = BayesNode(0, "parent1", size=2)
    pa2 = BayesNode(1, "parent2", size=2)

    pa1.state_names = ['0', '1']
    pa2.state_names = ['0', '1']

    cn = CNot(2, "a_cnot", False, pa1, pa2, True, True)

    print("pa1 state names: ", pa1.state_names)
    print("pa2 state names: ", pa2.state_names)
    print("cnot state names: ", cn.state_names)
    print(cn.potential)
    print(cn.potential.get_total_probs())
Example #9
0
        """
        if self.flipped_by_0:
            x = 1 - pa1_st
        else:  # flipped by 1
            x = pa1_st
        if self.pa1_is_control:
            bit0 = pa1_st
            bit1 = (x + pa2_st)//2
        else:  # pa2 is control
            bit0 = (x + pa2_st)//2
            bit1 = pa2_st
        foc_st = bit0 + 2*bit1
        return foc_st


if __name__ == "__main__":
    pa1 = BayesNode(0, "parent1", size=2)
    pa2 = BayesNode(1, "parent2", size=2)

    pa1.state_names = ['0', '1']
    pa2.state_names = ['0', '1']

    cn = CNot(2, "a_cnot",
        False, pa1, pa2, True, True)

    print("pa1 state names: ", pa1.state_names)
    print("pa2 state names: ", pa2.state_names)
    print("cnot state names: ", cn.state_names)
    print(cn.potential)
    print(cn.potential.get_total_probs())
Example #10
0
if __name__ == "__main__":

    num_of_comps = 1

    tau_mag = .5
    tau_degs = 35
    rho_degs = 45

    if num_of_comps == 1:
        size1 = 3
        size2 = 4
        max_n_sum = 5
        pa1 = BayesNode(0, "parent1", size=size1)
        pa2 = BayesNode(1, "parent2", size=size2)

        pa1.state_names = [str(k) for k in range(size1)]
        pa2.state_names = [str(k) for k in range(size2)]

        bs = BeamSplitter(2, "a_bs", pa1, pa2,
                tau_mag, tau_degs, rho_degs, num_of_comps, max_n_sum)

        print("pa1 state names: ", pa1.state_names)
        print("pa2 state names: ", pa2.state_names)
        print("bs state names: ", bs.state_names)
        print(bs.potential)
        print("full dict of total probs: ",
              bs.potential.get_total_probs())
        print("brief dict of total probs: ",
              bs.potential.get_total_probs(brief=True))
    elif num_of_comps == 2:
        size1 = 6
Example #11
0
        elif n == 1 and m == 1:
            x = c
            y = -theta_hat[3]*s
        elif n == 0 and m == 1:
            x = theta_hat[2]*s
            y = theta_hat[1]*s
        elif n == 1 and m == 0:
            x = -theta_hat[2]*s
            y = theta_hat[1]*s
        else:
            x = 0
            y = 0

        s = math.sin(rads[0])
        c = math.cos(rads[0])

        return (c*x - s*y) + 1j*(c*y + s*x)


if __name__ == "__main__":
    pa_nd = BayesNode(0, "pa_nd", size=2)
    pa_nd.state_names = ['0', '1']

    thetas_degs = [20, 30, 40, 60]
    qr = QubitRot(1, "rot", pa_nd, thetas_degs)

    print("pa_nd state names: ", pa_nd.state_names)
    print("qubit rot state names: ", qr.state_names)
    print(qr.potential)
    print(qr.potential.get_total_probs())
Example #12
0
        self.theta_degs = theta_degs
        self.occ_nums = occ_nums

        BayesNode.__init__(self, id_num, name, size=pa_nd.size)
        self.add_parent(pa_nd)

        self.state_names = pa_nd.state_names

        pot = DiscreteCondPot(True, [pa_nd, self], bias=0)
        self.potential = pot

        theta_rads = theta_degs*math.pi/180
        for k in range(pa_nd.size):
            if not occ_nums:
                phase = theta_rads
            else:
                num = int(pa_nd.state_names[k])
                phase = num*theta_rads
            self.potential[k, k] = cmath.exp(1j*phase)

if __name__ == "__main__":
    pa_nd = BayesNode(0, "pa_nd", size=4)
    pa_nd.state_names = [str(k) for k in range(4)]

    p_sh = PhaseShifter(1, "pshifter", pa_nd, 30, occ_nums=True)

    print("pa_nd state names: ", pa_nd.state_names)
    print("phase shifter state names: ", p_sh.state_names)
    print(p_sh.potential)
    print(p_sh.potential.get_total_probs())