コード例 #1
0
    def get_value_amplitude(self, oracle):
        circuit = self.__build_circuit_count(self.key_bits, self.value_bits,
                                             self.f, oracle)
        probs = get_probs((circuit, None, None), 'sim', False)
        ordered_probs = sorted(probs.items(), key=lambda x: x[1], reverse=True)
        print("number of outcomes:", len(ordered_probs))
        print("probabilities = ", ordered_probs)

        counts = sorted(list(
            map(lambda item: (int(item[0][:self.precision_bits], 2), item[1]),
                ordered_probs)),
                        key=lambda x: x[1],
                        reverse=True)
        print("counts = ", counts)

        combined_frequency = {}
        for k, v in counts:
            combined_frequency[k] = np.round(
                combined_frequency.get(k, 0) + v, 4)
        sorted_counts = sorted(combined_frequency.items(),
                               key=lambda x: x[1],
                               reverse=True)
        print("combined_frequencies = ", sorted_counts)

        sines = {}
        for k, v in counts:
            key = np.round(np.cos(np.pi * k / 2**self.precision_bits)**2, 4)
            sines[key] = sines.get(key, 0) + v
        sorted_sines = sorted(sines.items(), key=lambda x: x[1], reverse=True)
        print("sines = ", sorted_sines)

        estimate = round(sorted_sines[0][0], 4)
        print("Amplitude Estimate = ", estimate)
        print("Uniformly Scaled Estimate = ", 2**self.key_bits * estimate)
        return estimate
コード例 #2
0
    def get_value_for_key(self, key, neg = False):
        circuit = self.__build_circuit(self.key_bits, self.value_bits, self.f, key)
        probs = get_probs((circuit, None, None), 'sim', False)

        from qiskit.tools import visualization
        visualization.plot_histogram(probs)

        ordered_probs = sorted(probs.items(), key=lambda x: x[1], reverse=True)
        print("Probabilities: ", ordered_probs)

        _, outcomes = self.__process(self.key_bits, self.value_bits, probs, neg)
        ordered_outcomes = sorted(outcomes.items(), key=lambda x: x[1], reverse=True)

        print("Outcomes", ordered_outcomes)
        return ordered_probs[0][0]
コード例 #3
0
    def get_value_distribution(self):
        circuit = self.__build_circuit(self.key_bits, self.value_bits, self.f, None, None)
        probs = get_probs((circuit, None, None), 'sim', False)

        ordered_probs = sorted(probs.items(), key=lambda x: x[1], reverse=True)
        print("Probabilities: ", ordered_probs)

        v_freq = self.__process_values(self.key_bits, self.value_bits, probs)

        from qiskit.tools import visualization
        visualization.plot_histogram(v_freq)

        ordered_freq = sorted(v_freq.items(), key=lambda x: x[1], reverse=True)

        print("Value Distribution", ordered_freq)
コード例 #4
0
        cry(2**(i + 1) * theta, qc, q[i], t[0])

    return qc, None, None


if __name__ == "__main__":

    phi = 0.152
    theta = 0.235

    qc, _, _ = build_circuit(3, phi, theta)

    # from qiskit.tools.visualization import plot_circuit
    # plot_circuit(qc)

    hist = util.get_probs((qc, None, None), 'sim')
    print("Probabilities:", hist)

    visualization.plot_histogram(hist)

    print("sqrt(1/8)*cos(phi) = ", np.round(math.sqrt(1 / 8) * np.cos(phi), 5))
    print("sqrt(1/8)*sin(phi) = ", np.round(math.sqrt(1 / 8) * np.sin(phi), 5))

    print("sqrt(1/8)*cos(phi + theta) = ",
          np.round(math.sqrt(1 / 8) * np.cos(phi + theta), 5))
    print("sqrt(1/8)*sin(phi + theta) = ",
          np.round(math.sqrt(1 / 8) * np.sin(phi + theta), 5))

    print("sqrt(1/8)*cos(phi + 2*theta) = ",
          np.round(math.sqrt(1 / 8) * np.cos(phi + 2 * theta), 5))
    print("sqrt(1/8)*sin(phi + 2*theta) = ",
コード例 #5
0
    qc.x(q[1])
    qc.x(q[2])
    qc.x(q[3])
    qc.h(q[0])
    qc.h(q[1])
    qc.h(q[2])
    qc.h(q[3])


def controlled(qc, ctrl, anc, tgt, cc_gate = lambda qc, c1, c2, t: qc.ccx(c1, c2, t), c_gate = lambda qc, c, t: qc.cx(c, t)):
    n = len(ctrl)

    # compute
    cc_gate(qc, ctrl[0], ctrl[1], anc[0])
    for i in range(2, n):
        cc_gate(qc, ctrl[i], anc[i-2], anc[i-1])

    # copy
    c_gate(qc, anc[n-2], tgt[0])

    # uncompute
    for i in range(n-1, 1, -1):
        cc_gate(qc, ctrl[i], anc[i-2], anc[i-1])
    cc_gate(qc, ctrl[0], ctrl[1], anc[0])


if __name__ == "__main__":
    hist = util.get_probs(build_circuit(15), 'sim')
    print(hist)
    visualization.plot_histogram(hist)
コード例 #6
0
    for i in range(0, n):
        if is_bit_not_set(m, i):
            qc.x(q[n - 1 - i])

    util.controlled_X(qc, q, a, t)

    for i in range(0, n):
        if is_bit_not_set(m, i):
            qc.x(q[n - 1 - i])


def diffusion(qc, q, a):
    for i in range(0, len(q)):
        qc.h(q[i])
        qc.x(q[i])

    # controlled Z
    util.controlled_Z(qc, [q[i]
                           for i in range(0,
                                          len(q) - 1)], a, [q[len(q) - 1]])

    for i in range(0, len(q)):
        qc.x(q[i])
        qc.h(q[i])


if __name__ == "__main__":
    hist = util.get_probs(build_circuit(5, 31), 'sim')
    print(hist)
    visualization.plot_histogram(hist)