|
| 1 | +# https://github.com/rupansh/QuantumComputing/blob/master/rippleadd.py |
| 2 | +# https://en.wikipedia.org/wiki/Adder_(electronics)#Full_adder |
| 3 | +# https://en.wikipedia.org/wiki/Controlled_NOT_gate |
| 4 | + |
| 5 | +from qiskit import QuantumCircuit, execute, Aer |
| 6 | +from qiskit.providers import BaseBackend |
| 7 | + |
| 8 | + |
| 9 | +def store_two_classics(val1: int, val2: int) -> (QuantumCircuit, str, str): |
| 10 | + """ |
| 11 | + Generates a Quantum Circuit which stores two classical integers |
| 12 | + Returns the circuit and binary representation of the integers |
| 13 | + """ |
| 14 | + x, y = bin(val1)[2:], bin(val2)[2:] # Remove leading '0b' |
| 15 | + |
| 16 | + # Ensure that both strings are of the same length |
| 17 | + if len(x) > len(y): |
| 18 | + y = y.zfill(len(x)) |
| 19 | + else: |
| 20 | + x = x.zfill(len(y)) |
| 21 | + |
| 22 | + # We need (3 * number of bits in the larger number)+1 qBits |
| 23 | + # The second parameter is the number of classical registers, to measure the result |
| 24 | + circuit = QuantumCircuit((len(x) * 3) + 1, len(x) + 1) |
| 25 | + |
| 26 | + # We are essentially "not-ing" the bits that are 1 |
| 27 | + # Reversed because its easier to perform ops on more significant bits |
| 28 | + for i in range(len(x)): |
| 29 | + if x[::-1][i] == "1": |
| 30 | + circuit.x(i) |
| 31 | + for j in range(len(y)): |
| 32 | + if y[::-1][j] == "1": |
| 33 | + circuit.x(len(x) + j) |
| 34 | + |
| 35 | + return circuit, x, y |
| 36 | + |
| 37 | + |
| 38 | +def full_adder( |
| 39 | + circuit: QuantumCircuit, |
| 40 | + input1_loc: int, |
| 41 | + input2_loc: int, |
| 42 | + carry_in: int, |
| 43 | + carry_out: int, |
| 44 | +): |
| 45 | + """ |
| 46 | + Quantum Equivalent of a Full Adder Circuit |
| 47 | + CX/CCX is like 2-way/3-way XOR |
| 48 | + """ |
| 49 | + circuit.ccx(input1_loc, input2_loc, carry_out) |
| 50 | + circuit.cx(input1_loc, input2_loc) |
| 51 | + circuit.ccx(input2_loc, carry_in, carry_out) |
| 52 | + circuit.cx(input2_loc, carry_in) |
| 53 | + circuit.cx(input1_loc, input2_loc) |
| 54 | + |
| 55 | + |
| 56 | +def ripple_adder( |
| 57 | + val1: int, val2: int, backend: BaseBackend = Aer.get_backend("qasm_simulator") |
| 58 | +): |
| 59 | + """ |
| 60 | + Quantum Equivalent of a Ripple Adder Circuit |
| 61 | + Uses qasm_simulator backend by default |
| 62 | +
|
| 63 | + Currently only adds 'emulated' Classical Bits |
| 64 | + but nothing prevents us from doing this with hadamard'd bits :) |
| 65 | +
|
| 66 | + Only supports adding +ve Integers |
| 67 | +
|
| 68 | + >>> ripple_adder(3, 4) |
| 69 | + 7 |
| 70 | + >>> ripple_adder(10, 4) |
| 71 | + 14 |
| 72 | + >>> ripple_adder(-1, 10) |
| 73 | + Traceback (most recent call last): |
| 74 | + ... |
| 75 | + ValueError: Both Integers must be positive! |
| 76 | + """ |
| 77 | + |
| 78 | + if val1 < 0 or val2 < 0: |
| 79 | + raise ValueError("Both Integers must be positive!") |
| 80 | + |
| 81 | + # Store the Integers |
| 82 | + circuit, x, y = store_two_classics(val1, val2) |
| 83 | + |
| 84 | + """ |
| 85 | + We are essentially using each bit of x & y respectively as full_adder's input |
| 86 | + the carry_input is used from the previous circuit (for circuit num > 1) |
| 87 | +
|
| 88 | + the carry_out is just below carry_input because |
| 89 | + it will be essentially the carry_input for the next full_adder |
| 90 | + """ |
| 91 | + for i in range(len(x)): |
| 92 | + full_adder(circuit, i, len(x) + i, len(x) + len(y) + i, len(x) + len(y) + i + 1) |
| 93 | + circuit.barrier() # Optional, just for aesthetics |
| 94 | + |
| 95 | + # Measure the resultant qBits |
| 96 | + for i in range(len(x) + 1): |
| 97 | + circuit.measure([(len(x) * 2) + i], [i]) |
| 98 | + |
| 99 | + res = execute(circuit, backend, shots=1).result() |
| 100 | + |
| 101 | + # The result is in binary. Convert it back to int |
| 102 | + return int(list(res.get_counts().keys())[0], 2) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + import doctest |
| 107 | + |
| 108 | + doctest.testmod() |
0 commit comments