コード例 #1
0
def prep_CNOT_control_CQC(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    q1.cnot(q2)
    q2.measure()
    return q1
コード例 #2
0
    def testFactoryNew(self):
        with CQCConnection("Alice", appID=1) as alice:
            # Should return a list of qubits with consecutive qubit ids
            alice.set_pending(True)
            qubit(alice)
            qubits = alice.flush_factory(10, do_sequence=False)
            # It is preferable to use the following however:
            # qubits = alice.allocate_qubits(10)
            alice.set_pending(False)
            # Checking the factory and the measure, factory should not log any commands
            lastEntries = get_last_entries(11)
            factoryEntry = lastEntries[0]
            self.assertEqual(factoryEntry["node_name"], "Alice")
            factory_cqc_header = factoryEntry["cqc_header"]
            self.assertEqual(factory_cqc_header["type"], CQC_TP_FACTORY)
            expected_length = CQCFactoryHeader.HDR_LENGTH + CQC_CMD_HDR_LENGTH
            self.assertEqual(factory_cqc_header["header_length"],
                             expected_length)
            self.assertEqual(factoryEntry["factory_iterations"], 10)

            for i in range(1, 11):
                xEntry = lastEntries[i]
                x_cmd_cmd_header = xEntry["cmd_header"]
                self.assertEqual(x_cmd_cmd_header["instruction"], CQC_CMD_NEW)

            curID = qubits[0]._qID
            for q in qubits[1:]:
                self.assertEqual(q._qID, curID + 1)
                curID = q._qID
コード例 #3
0
def prep_CNOT_target_CQC(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    q1.cnot(q2)
    q1.measure()
    return q2
コード例 #4
0
    def test_epr_for_last_position(self):
        with CQCConnection("Alice") as alice:
            with CQCConnection("Bob") as bob:
                alice_qubits = []
                bob_qubits = []
                for _ in range(Settings.CONF_MAXQUBITS - 1):
                    q = qubit(alice)
                    alice_qubits.append(q)
                for _ in range(Settings.CONF_MAXQUBITS - 1):
                    q = qubit(bob)
                    bob_qubits.append(q)

                q = alice.createEPR("Bob", remote_appID=bob._appID)
                alice_qubits.append(q)
                q = bob.recvEPR()
                bob_qubits.append(q)

                with self.assertRaises(CQCNoQubitError):
                    alice.createEPR("Bob", remote_appID=bob._appID)

                # remove one qubit from Alice
                alice_qubits[0].measure()

                with self.assertRaises(CQCNoQubitError):
                    alice.createEPR("Bob", remote_appID=bob._appID)

                # remove one qubit from Bob
                bob_qubits[0].measure()

                alice.createEPR("Bob", remote_appID=bob._appID)
                bob.recvEPR()
コード例 #5
0
    def testFactoryCPHASE(self):
        with CQCConnection("Alice", appID=1) as alice:
            q1 = qubit(alice)
            q2 = qubit(alice)
            alice.set_pending(True)
            q1.cphase(q2)
            alice.flush_factory(10, do_sequence=False)
            alice.set_pending(False)

            entries = get_last_entries(11)
            factoryEntry = entries[0]
            self.assertEqual(factoryEntry["node_name"], "Alice")
            factory_cqc_header = factoryEntry["cqc_header"]
            self.assertEqual(factory_cqc_header["type"], CQC_TP_FACTORY)
            expected_length = CQCFactoryHeader.HDR_LENGTH + CQC_CMD_HDR_LENGTH + CQCXtraQubitHeader.HDR_LENGTH
            self.assertEqual(factory_cqc_header["header_length"],
                             expected_length)
            self.assertEqual(factoryEntry["factory_iterations"], 10)

            for i in range(1, 11):
                xEntry = entries[i]
                x_cmd_cmd_header = xEntry["cmd_header"]
                self.assertEqual(x_cmd_cmd_header["instruction"],
                                 CQC_CMD_CPHASE)
                self.assertEqual(x_cmd_cmd_header["qubit_id"], q1._qID)
                x = xEntry["xtra_header"]
                self.assertEqual(x["qubit_id"], q2._qID)
コード例 #6
0
    def test_more_than_max(self):
        with CQCConnection("Alice") as alice:
            for _ in range(Settings.CONF_MAXQUBITS):
                qubit(alice)

            with self.assertRaises(CQCNoQubitError):
                qubit(alice)
コード例 #7
0
    def _atomic_flip(self, candidate1, candidate2, coeff):
        """
        Performs the basic biased coinflip between two parties.
        :param candidate1: the first party id.
        :param candidate2: the second party id.
        :param coeff: bias.
        :return: the winner id.
        """
        with CQCConnection(candidate1) as Alice:
            qA = qubit(Alice)
            qB = qubit(Alice)

            # Bias
            angle = 2 * math.acos(coeff)
            step = int(angle * 256 / (2 * math.pi))
            qA.rot_Y(step)
            qA.cnot(qB)
            qB.X()

            # Send qubit qB to Bob.
            Alice.sendQubit(qB, candidate2)

            # Measure the qubits.
            measured_value = qA.measure()

            with CQCConnection(candidate2) as Bob:
                qB = Bob.recvQubit()
                bob_value = qB.measure()
                assert measured_value + bob_value == 1

            if measured_value == 1:  # `candidate1` is a winner.
                return candidate1
            else:
                return candidate2
コード例 #8
0
 def test_without_context(self):
     for _ in range(Settings.CONF_MAXQUBITS):
         cqc = CQCConnection("Alice")
         self.qubits.append(qubit(cqc))
     with self.assertRaises(CQCNoQubitError):
         cqc = CQCConnection("Alice")
         qubit(cqc)
コード例 #9
0
 def testFactoryCNOTFalse(self):
     with CQCConnection("Alice", appID=1) as alice:
         q1 = qubit(alice)
         qubit(alice)
         with self.assertRaises(CQCUnsuppError):
             alice.set_pending(True)
             q1.cnot(q1)
             alice.flush_factory(10, do_sequence=False)
             alice.set_pending(False)
コード例 #10
0
 def testCNotRemote(self):
     with CQCConnection("Alice", appID=1) as alice:
         # The appId in xtra_header['app_id'] is not 2 when testing.
         # In fact, doing this code in a real application result in an error as of 2018-03-12
         with CQCConnection("Bob", appID=2) as bob:
             q1 = qubit(alice)
             q2 = qubit(bob)
             with self.assertRaises(CQCUnsuppError):
                 q1.cnot(q2)
コード例 #11
0
def prep_CPHASE_target_CQC(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    q2.H()
    q1.cphase(q2)
    q2.H()
    q1.measure()
    return q2
コード例 #12
0
def prep_CPHASE_control_CQC(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    q2.H()
    q1.cphase(q2)
    q2.H()
    q2.measure()
    return q1
コード例 #13
0
 def testGetTime(self):
     # Test Get time
     q1 = qubit(self.cqc)
     time.sleep(3)
     q2 = qubit(self.cqc)
     t1 = q1.getTime()
     t2 = q2.getTime()
     self.assertEqual(t2 - t1, 3)
     q1.measure()
     q2.measure()
コード例 #14
0
def prep_CNOT_control_CQC_FACTORY_even(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    cqc.set_pending(True)
    q1.cnot(q2)
    cqc.flush_factory(4)
    cqc.set_pending(False)
    q2.measure()
    return q1
コード例 #15
0
def prep_CNOT_target_CQC_FACTORY_odd(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    cqc.set_pending(True)
    q1.cnot(q2)
    cqc.flush_factory(3)
    cqc.set_pending(False)
    q1.measure()
    return q2
コード例 #16
0
 def testNew(self):
     with CQCConnection("Alice", appID=0, pend_messages=True) as cqc:
         qubit(cqc)
         qubits = cqc.flush_factory(self.iterations)
         self.assertEqual(len(qubits), self.iterations)
         for q in qubits:
             q.X()
             q.measure()
         results = cqc.flush()
         self.assertEqual(results, [1] * self.iterations)
         self.assertEqual(len(cqc.pending_messages), 0)
コード例 #17
0
 def testGetTime(self):
     with CQCConnection("Alice", appID=1) as cqc:
         # Test Get time
         q1 = qubit(cqc)
         time.sleep(3)
         q2 = qubit(cqc)
         t1 = q1.getTime()
         t2 = q2.getTime()
         self.assertEqual(t2 - t1, 3)
         q1.measure()
         q2.measure()
コード例 #18
0
def prep_CPHASE_control_CQC_FACTORY_odd(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    q2.H()
    cqc.set_pending(True)
    q1.cphase(q2)
    cqc.flush_factory(3)
    cqc.set_pending(False)
    q2.H()
    q2.measure()
    return q1
コード例 #19
0
def prep_CPHASE_target_CQC_FACTORY_even(cqc):
    q1 = qubit(cqc)
    q2 = qubit(cqc)
    q1.H()
    q2.H()
    cqc.set_pending(True)
    q1.cphase(q2)
    cqc.flush_factory(4)
    cqc.set_pending(False)
    q2.H()
    q1.measure()
    return q2
コード例 #20
0
 def testNewQubit(self):
     with CQCConnection("Alice", appID=1) as alice:
         qubit(alice, block=False, notify=False)
         lastEntry = get_last_entries(1)[0]
         cmd_header = lastEntry["cmd_header"]
         self.assertEqual(lastEntry["node_name"], "Alice")
         self.assertEqual(cmd_header["instruction"], CQC_CMD_NEW)
         self.assertEqual(cmd_header["block"], False)
         self.assertEqual(cmd_header["notify"], False)
         cqc_header = lastEntry["cqc_header"]
         self.assertEqual(cqc_header["type"], CQC_TP_COMMAND)
         self.assertEqual(cqc_header["header_length"], CQC_CMD_HDR_LENGTH)
         self.assertEqual(cqc_header["app_id"], 1)
コード例 #21
0
def prep_send_CQC(cqc):
    with CQCConnection("Alice", appID=1) as Alice:
        qA = qubit(cqc)
        qB = qubit(cqc)
        qA.H()
        qA.cnot(qB)
        cqc.sendQubit(qA, "Alice", remote_appID=1)
        qA = Alice.recvQubit()
        m = qA.measure()
        if m == 1:
            qB.X()
        qB.H()
    return qB
コード例 #22
0
def prep_I_CQC_FACTORY(cqc):
    q = qubit(cqc)
    cqc.set_pending(True)
    q.I()
    cqc.flush_factory(3)
    cqc.set_pending(False)
    return q
コード例 #23
0
def prep_K_CQC_FACTORY_EVEN(cqc):
    q = qubit(cqc)
    cqc.set_pending(True)
    q.K()
    cqc.flush_factory(4)
    cqc.set_pending(False)
    return q
コード例 #24
0
def prep_recv_CQC(cqc):
    with CQCConnection("Alice", appID=1) as Alice:
        qA = qubit(Alice)
        qA.H()
        Alice.sendQubit(qA, "Bob")
        qB = cqc.recvQubit()
    return qB
コード例 #25
0
def prep_ROT_X(cqc):
    q = qubit(cqc)
    cqc.set_pending(True)
    q.rot_X(step=4)
    cqc.flush_factory(4)
    cqc.set_pending(False)
    return q
コード例 #26
0
def main():

    # Initialize the connection
    with CQCConnection("Alice") as Alice:
        Alice.closeClassicalServer()

        # Generate a key
        k = random.randint(0, 1)
        chosen_basis = random.randint(0, 1)

        # Create a qubit
        q = qubit(Alice)

        # Encode the key in the qubit
        if k == 1:
            q.X()

        # Encode in H basis if basis = 1
        if chosen_basis == 1:
            q.H()

        # Send qubit to Bob (via Eve)
        Alice.sendQubit(q, "Eve")

        # Encode and send a classical message m to Bob
        m = 1
        enc = (m + k) % 2
        send_message(Alice, "Bob", bytes([enc]))

        print("\nAlice basis={}".format(BASIS[chosen_basis]))
        print("Alice key={}".format(k))
        print("Alice sent the message m={} to Bob".format(m))
コード例 #27
0
ファイル: aliceTest.py プロジェクト: andrebreis/SimulaQron
def main():

    # Initialize the connection
    with CQCConnection("Alice") as Alice:

        # Make an EPR pair with Bob
        qA = Alice.createEPR("Bob")

        # Create a qubit to teleport
        q = qubit(Alice)

        # Prepare the qubit to teleport in |+>
        q.H()

        # Apply the local teleportation operations
        q.cnot(qA)
        q.H()

        # Measure the qubits
        a = q.measure()
        b = qA.measure()
        to_print = "App {}: Measurement outcomes are: a={}, b={}".format(
            Alice.name, a, b)
        print("|" + "-" * (len(to_print) + 2) + "|")
        print("| " + to_print + " |")
        print("|" + "-" * (len(to_print) + 2) + "|")

        # Send corrections to Bob
        Alice.sendClassical("Bob", [a, b])
コード例 #28
0
 def testSingleGates(self):
     with CQCConnection("Alice", pend_messages=True) as alice:
         with CQCConnection("Bob", appID=1, pend_messages=True) as bob:
             q = qubit(alice)
             q.X()
             q.measure(inplace=True)
             r = alice.flush()
             self.assertEqual(len(r), 2)
             self.assertEqual(r[1], 1)
             q.reset()
             q.Y()
             q.measure(inplace=True)
             r = alice.flush()
             self.assertEqual(r, [1])
             q.reset()
             q.Z()
             q.measure(inplace=True)
             r = alice.flush()
             self.assertEqual(r, [0])
             q.reset()
             q.H()
             q.H()
             q.measure()
             r = alice.flush()
             self.assertEqual(r, [0])
             self.assertEqual(alice.pending_messages, [])
             self.assertEqual(bob.pending_messages, [])
             alice.flush()
             bob.flush()
コード例 #29
0
    def testSend(self):
        with CQCConnection("Alice", pend_messages=True) as alice:
            with CQCConnection("Bob", appID=1, pend_messages=True) as bob:
                qA = qubit(alice)
                qAs = alice.flush_factory(10)
                self.assertIsNone(qA._qID)
                self.assertFalse(qA.check_active())

                for q in qAs:
                    self.assertTrue(q._active)
                    alice.sendQubit(q, name="Bob", remote_appID=1)
                    self.assertTrue(q._active)

                alice.flush()
                qB = bob.recvQubit()
                qBs = bob.flush_factory(10)
                self.assertIsNone(qB._qID)
                self.assertFalse(qB.check_active())

                for q in qAs:
                    self.assertFalse(q._active)

                for i in range(1, 10):
                    self.assertEqual(qBs[i - 1]._qID + 1, qBs[i]._qID)
                bob.set_pending(False)
                for q in qBs:
                    self.assertEqual(q.measure(), 0)
                bob.set_pending(True)
                self.assertEqual(alice.pending_messages, [])
                self.assertEqual(bob.pending_messages, [])
                alice.flush()
                bob.flush()
コード例 #30
0
def main():

    n = parse_arguments()
    basis_string = ''
    bitstring = ''

    # Initialize the connection
    with CQCConnection("Alice") as Alice:

        for i in range(0, n):

            # Generate a key
            k = random.randint(0, 1)
            bitstring += str(k)
            chosen_basis = random.randint(0, 1)
            basis_string += str(chosen_basis)

            # Create a qubit
            q = qubit(Alice)

            # Encode the key in the qubit
            if k == 1:
                q.X()

            # Encode in H basis if basis = 1
            if chosen_basis == 1:
                q.H()

            # Send qubit to Bob (via Eve)
            Alice.sendQubit(q, "Eve")
            receive_message(Alice)

        print("\nAlice basis={}".format(basis_string))
        print("Alice sent the key k={} to Bob".format(bitstring))