Exemplo n.º 1
0
def _get_sample_matrix_and_random_syndrome():
    ses = Session()

    logger.info("Trying to get isd parameters for {0}, {1}, {2}, {3}".format(
        ses.args.n, ses.args.k, ses.args.d, ses.args.w))
    h, _, syndromes, _, _, _ = rch.get_isd_systematic_parameters(
        ses.args.n, ses.args.k, ses.args.d, ses.args.w)
    ses.h = h
    ses.r = ses.args.n - ses.args.k
    syndrome = syndromes[randint(0, syndromes.shape[0] - 1)]
    ses.syndrome = syndrome
Exemplo n.º 2
0
def ex_w_classic():
    n = 8
    k = 4
    d = 4
    w = 2
    p = 2
    h, _, syndromes, errors, w, _ = rch.get_isd_systematic_parameters(
        n, k, d, w)
    s = syndromes[1]
    lee = LeeBrickell(h, s, w, p)
    e = lee.run()
    for k, v in lee.result.items():
        print(k, v)
    print(e)
 def test_bruteforce_benes(self, name, n, k, d, w):
     h, _, syndromes, errors, w, _ = rch.get_isd_systematic_parameters(
         n, k, d, w)
     self.logger.info(
         "Launching TEST w/ n = {0}, k = {1}, d = {2}, w = {3}".format(
             n, k, d, w))
     self.logger.debug("h = \n{0}".format(h))
     for i, s in enumerate(syndromes):
         with self.subTest(s=s):
             self.logger.info("Starting subtest w/ s {}".format(s))
             bru = BruteforceAlg(h, s, w, True, 'advanced', 'fpc')
             qc, result, e, accuracy = bru.run('basicaer', 'qasm_simulator')
             counts = result.get_counts()
             self.logger.debug(counts)
             self.assertGreater(accuracy, 2 / 3)
             np.testing.assert_array_equal(e, errors[i])
 def test_algorithm_fpc(self, name, n, k, d, w, p):
     h, _, syndromes, errors, w, _ = rch.get_isd_systematic_parameters(
         n, k, d, w)
     self.logger.info(
         "Launching TEST w/ n = {0}, k = {1}, d = {2}, w = {3}, p = {4}".
         format(n, k, d, w, p))
     self.logger.debug("h = \n{0}".format(h))
     for i, s in enumerate(syndromes):
         with self.subTest(s=s):
             self.logger.info("Starting subtest w/ s {}".format(s))
             lee = LeeBrickellMixedAlg(h, s, w, p, True, 'advanced', 'fpc')
             qc, result, e, accuracy = lee.run('aer', 'qasm_simulator')
             counts = result.get_counts()
             self.logger.debug(counts)
             self.assertGreater(accuracy, 2 / 3)
             np.testing.assert_array_equal(e, errors[i])
Exemplo n.º 5
0
    def test_fixed_v_benes(self, name, n, k, d, w, p):
        h, _, syndromes, errors, w, _ = rch.get_isd_systematic_parameters(
            n, k, d, w)
        self.logger.info(
            "Launching TEST w/ n = {0}, k = {1}, d = {2}, w = {3}, p = {4}".
            format(n, k, d, w, p))
        self.logger.debug("h = \n{0}".format(h))
        for i, s in enumerate(syndromes):
            with self.subTest(s=s):
                self.logger.info("Launching SUBTEST w/ s = {0}".format(s))
                lee = LeeBrickell(h, s, w, p)
                exp_e = lee.run()
                self.logger.debug(
                    "For s = {0}, w = {1}, p = {2} h = \n{3}\nerror is {4}".
                    format(s, w, p, h, exp_e))
                # Just a double check on the result of the classic algorithm
                np.testing.assert_array_almost_equal(exp_e, errors[i])
                self.logger.info(
                    "LeeBrickell Classic ended, preparing quantum")
                # for k, v in lee.result.items():
                #     print("{} -> {}".format(k, v))
                hr = lee.result['hr']
                perm = lee.result['perm']
                s_sig = lee.result['s_sig']
                u = lee.result['u']
                v = lee.result['v']
                exp_e_hat = lee.result['e_hat']
                exp_indexes = lee.result['indexes']
                # print(v)
                # print(s_sig)

                # QUANTUM
                wanted_sum = w - p
                shots = 4098
                # print("v =\n{}".format(v))
                # print("s_sig = ".format(s_sig))
                lb = LeeBrickellCircuit(v, s_sig, w, p, True, 'advanced',
                                        'benes')
                qc = lb.build_circuit()
                result = self.execute_qasm(qc, shots=shots)
                counts = result.get_counts()
                self.logger.info(counts)

                # BUILD ERROR VECTOR
                max_val = max(counts.values())
                max_val_status = max(counts, key=lambda key: counts[key])
                accuracy = max_val / shots
                self.assertGreater(accuracy, 2 / 3)
                error_positions = [
                    pos for pos, char in enumerate(max_val_status[::-1])
                    if char == '1'
                ]
                self.assertEqual(error_positions, list(exp_indexes))
                # self.logger.debug("Error positions {}".format(error_positions))
                # self.logger.debug("Expected error positions {}".format(indexes))
                v_extr = v[:, error_positions]
                sum_to_s = (v_extr.sum(axis=1) + s_sig) % 2
                sum_to_s_w = np.sum(sum_to_s)
                self.assertEqual(sum_to_s_w, wanted_sum)
                e_hat = np.concatenate((np.zeros(k), sum_to_s))
                for j in error_positions:
                    e_hat[j] = 1
                self.logger.debug(
                    "e_hat after error position is {}".format(e_hat))
                np.testing.assert_array_equal(e_hat, exp_e_hat)
                # print("expected e_hat is {}".format(exp_e_hat))
                e_hat_w = np.sum(e_hat)
                self.assertEqual(e_hat_w, w)
                e = np.mod(np.dot(e_hat, perm.T), 2)
                np.testing.assert_array_equal(e, exp_e)