Esempio n. 1
0
    def test_mutability(self):
        """Test the mutability of the weighted adder."""
        adder = WeightedAdder()

        with self.subTest(msg='missing number of state qubits'):
            with self.assertRaises(AttributeError):
                print(adder.draw())

        with self.subTest(msg='default weights'):
            adder.num_state_qubits = 3
            default_weights = 3 * [1]
            self.assertListEqual(adder.weights, default_weights)

        with self.subTest(msg='specify weights'):
            adder.weights = [3, 2, 1]
            self.assertSummationIsCorrect(adder)

        with self.subTest(msg='mismatching number of state qubits and weights'):
            with self.assertRaises(ValueError):
                adder.weights = [0, 1, 2, 3]
                print(adder.draw())

        with self.subTest(msg='change all attributes'):
            adder.num_state_qubits = 4
            adder.weights = [2, 0, 1, 1]
            self.assertSummationIsCorrect(adder)
 def build(self, qc, q, q_ancillas=None, params=None):
     instr = WeightedAdder(num_state_qubits=self.num_state_qubits,
                           weights=self.weights).to_instruction()
     qr = [q[i] for i in self.i_state + self.i_sum]
     if q_ancillas:
         qr += q_ancillas[:self.required_ancillas()]  # pylint:disable=unnecessary-comprehension
     qc.append(instr, qr)
Esempio n. 3
0
def wasaveqb(relative_numbers):
    from unqomp.examples.weightedadder import makeWeightedAdderWOExtraCtrlsQb, makesQiskitWA
    from qiskit.circuit.library.arithmetic import WeightedAdder

    vals = [1,2,3,2,5,6,5,3,4,5,8,2]
    n = 12

    circuit1 = makeWeightedAdderWOExtraCtrlsQb(n, vals).circuitWithUncomputation()

    qcirc = WeightedAdder(n, vals)
    print('WeightedAdder alt. impl., with regression bug ; ', end = '')

    #qiskit
    nb_qb_qi = qcirc.num_qubits
    nb_gates_qi = qcirc.decompose().decompose().decompose().decompose().decompose().count_ops()
    if not relative_numbers:
        print(str(nb_qb_qi) + ' ; ' + str(nb_gates_qi['cx'] + nb_gates_qi['u3']) + ' ; ' + str(nb_gates_qi['cx']) + ' ; ', end = '')

    #qiskit++
    nb_qb_mi = circuit1.num_qubits
    nb_gates_mi = circuit1.decompose().decompose().decompose().decompose().decompose().count_ops()
    if not relative_numbers:
        print(str(nb_qb_mi) + ' ; ' + str(nb_gates_mi['cx'] + nb_gates_mi['u3']) + ' ; ' + str(nb_gates_mi['cx']))

    if relative_numbers:
        print_relative_vals(nb_qb_qi, nb_gates_qi['cx'], nb_gates_qi['u3'], nb_qb_mi, nb_gates_mi['cx'], nb_gates_mi['u3'])

    # qiskit with bug fixed
    qiskitMCX = makesQiskitWA(n, vals)
    nb_qb_qi = qiskitMCX.num_qubits
    nb_gates_qi = qiskitMCX.decompose().decompose().decompose().decompose().decompose().count_ops()
    print('WeightedAdder alt, impl. *, regression bug fixed  ; ', end = '')
    if not relative_numbers:
        print(str(nb_qb_qi) + ' ; ' + str(nb_gates_qi['cx'] + nb_gates_qi['u3']) + ' ; ' + str(nb_gates_qi['cx']) + str(' ; '), end = '')
        print(str(nb_qb_mi) + ' ; ' + str(nb_gates_mi['cx'] + nb_gates_mi['u3']) + ' ; ' + str(nb_gates_mi['cx']))
    else:
        print_relative_vals(nb_qb_qi, nb_gates_qi['cx'], nb_gates_qi['u3'], nb_qb_mi, nb_gates_mi['cx'], nb_gates_mi['u3'])
Esempio n. 4
0
def weightedAdder(nb_vars_max=32):
    weights = [
        1, 2, 3, 2, 5, 6, 5, 3, 4, 5, 8, 2, 3, 6, 7, 5, 4, 3, 4, 9, 4, 5, 7, 1,
        2, 3, 4, 5, 6, 7, 8, 6
    ]

    if nb_vars_max > len(weights):
        print("More variables than weights. Weigths should be changed")
        return

    from unqomp.examples.weightedadder import makeWeightedAdderWOExtraCtrlsQb, makesQiskitWA, makeWeightedAdder
    from qiskit.circuit.library.arithmetic import WeightedAdder

    nb_vars_t = []
    nb_qb_qi = []
    nb_qb_mi = []
    nb_qb_bq = []
    nb_qb_mib = []
    nb_gates_qi = []
    nb_gates_mi = []
    nb_gates_bq = []
    nb_gates_mib = []

    for nb_vars in range(3, nb_vars_max, 3):
        circuit1 = makeWeightedAdderWOExtraCtrlsQb(nb_vars, weights[:nb_vars])
        circuit1 = circuit1.circuitWithUncomputation()

        qiskitMCX = makesQiskitWA(nb_vars, weights[:nb_vars])

        buggyQ = WeightedAdder(nb_vars, weights[:nb_vars])

        circuit2 = makeWeightedAdder(nb_vars, weights[:nb_vars])
        circuit2 = circuit2.circuitWithUncomputation()

        nb_vars_t.append(nb_vars)
        #qiskit
        nb_qb_qi.append(qiskitMCX.num_qubits)
        nb_gates_qi.append(qiskitMCX.decompose().decompose().decompose().
                           decompose().decompose().count_ops())

        #mine
        nb_qb_mi.append(circuit1.num_qubits)
        nb_gates_mi.append(circuit1.decompose().decompose().decompose().
                           decompose().decompose().count_ops())

        #buggy qiskit
        nb_qb_bq.append(buggyQ.num_qubits)
        nb_gates_bq.append(buggyQ.decompose().decompose().decompose().
                           decompose().decompose().count_ops())

        #mine w more qb
        nb_qb_mib.append(circuit2.num_qubits)
        nb_gates_mib.append(circuit2.decompose().decompose().decompose().
                            decompose().decompose().count_ops())

    with open('evaluation/plot_values/weightedadder.csv', 'w') as f:
        sys.stdout = f  # Change the standard output to the file we created.
        print(
            "input size; n_qb Qiskit without regression bug; n_gates Qiskit without regression bug; n_cx_gates Qiskit without regression bug;  n_qb Unqomp; n_gates Unqomp ; n_cx_gates Uncomp; n_qb Qiskit with regression bug; n_gates Qiskit with regression bug ; n_cx_gates Qiskit with regression bug; n_qb Unqomp alt. impl.; n_gates Unqomp alt. impl.; n_cx_gates Unqomp alt. impl."
        )
        for i in range(len(nb_vars_t)):
            print(nb_vars_t[i], ";", nb_qb_qi[i], ";",
                  nb_gates_qi[i]['u3'] + nb_gates_qi[i]['cx'], ";",
                  nb_gates_qi[i]['cx'], ";", nb_qb_mi[i], ";",
                  nb_gates_mi[i]['u3'] + nb_gates_mi[i]['cx'], ";",
                  nb_gates_mi[i]['cx'], ";", nb_qb_bq[i], ";",
                  nb_gates_bq[i]['u3'] + nb_gates_bq[i]['cx'], ";",
                  nb_gates_bq[i]['cx'], ";", nb_qb_mib[i], ";",
                  nb_gates_mib[i]['u3'] + nb_gates_mib[i]['cx'], ";",
                  nb_gates_mib[i]['cx'])
        sys.stdout = original_stdout  # Reset the standard output to its original value
Esempio n. 5
0
 def test_summation(self, weights):
     """Test the weighted adder on some examples."""
     adder = WeightedAdder(len(weights), weights)
     self.assertSummationIsCorrect(adder)