{{ message }}

Instantly share code, notes, and snippets.

primaryobjects/logic.py

Created Jul 31, 2021
Quantum Computing logic gates for XOR, AND, NAND, OR using Qiskit.
 from qiskit import qiskit, QuantumCircuit def execute(func): print('0 0: {}'.format(func(0, 0))) print('0 1: {}'.format(func(0, 1))) print('1 0: {}'.format(func(1, 0))) print('1 1: {}'.format(func(1, 1))) def xor(a, b): """ XOR gate https://quantum-computing.ibm.com/composer/files/new?initial=N4IgdghgtgpiBcIAaB5ASiANCAjhAzlAiCgAoCiAcgIoCCAygLIAEATAHQAMA3ADpgBLMAGMANgFcAJjGa9cMUQIBGARnZDhcvmH44ATjADmzHAG0AzAF1twg8eEXr-fgA8Tpzk7BuzKr0og9PQEYPXdPbQCgkLDfL2EfD0tMM1Z4xL8U0zTIwODQ9xz%2BWAJxA0LLZgBaAD5mBxysEGl8WwEABwAXAQB7MGIQAF8gA """ qc = QuantumCircuit(3, 1) # Set up the registers if a: qc.x(0) if b: qc.x(1) qc.barrier() # XOR qc.cx(0, 2) qc.cx(1, 2) qc.barrier() # Measure qc.measure(2, 0) #print('Depth: {}'.format(qc.depth())) job = qiskit.execute(qc, qiskit.BasicAer.get_backend('qasm_simulator')) return job.result().get_counts() def and_gate(a, b): """ AND gate https://quantum-computing.ibm.com/composer/files/new?initial=N4IgdghgtgpiBcICCA5AIiANCAjhAzlAiAPIAKAoigIpIDKAsgAQBMAdAAwDcAOmAJZgAxgBsArgBMYTHrhgj%2BAIwCMbQUNm8wfHACcYAcyY4A2gGYAulqH6jQ81b58AHsZMdHYRRF27%2BMXTcPLVdTZU9vX39AsM8hIVD3C0xYlJMWCJ8-AKDMqJzYrUjsmPTPWAIxfTcMpgBaAD4mewyuLBApfBt%2BAAcAF34AezBiEABfIA """ qc = QuantumCircuit(3, 1) # Set up the registers if a: qc.x(0) if b: qc.x(1) qc.barrier() # AND qc.ccx(0, 1, 2) qc.barrier() # Measure qc.measure(2, 0) #print('Depth: {}'.format(qc.depth())) job = qiskit.execute(qc, qiskit.BasicAer.get_backend('qasm_simulator')) return job.result().get_counts() def nand(a, b): """ NAND gate https://quantum-computing.ibm.com/composer/files/new?initial=N4IgdghgtgpiBcIByBBJAREAaEBHCAzlAiAPIAKAokgIooDKAsgAQBMAdAAwDcAOmAEswAYwA2AVwAmMZrzwxRAgEYBGdkOFy%2BYfrgBOMAObNcAbQDMAXW3CDx4Rev9%2BADxOnOTsEoh69AmD13T203MxUvHz8AoPCvYWEwj0ssONTTVi8kzO1YQnEDd0zmAFoAPmYHHOwQaQJbAQAHABcBAHswEhAAXyA """ qc = QuantumCircuit(3, 1) # Set up the registers if a: qc.x(0) if b: qc.x(1) qc.barrier() # NAND qc.ccx(0, 1, 2) qc.x(2) qc.barrier() # Measure qc.measure(2, 0) job = qiskit.execute(qc, qiskit.BasicAer.get_backend('qasm_simulator')) return job.result().get_counts() def or_gate(a, b): """ OR gate https://quantum-computing.ibm.com/composer/files/new?initial=N4IgdghgtgpiBcIDyAlEAaEBHCBnKCyACgKIByAigIIDKAsgAQBMAdAAwDcAOmAJZgBjADYBXACYwGXbDCG8ARgEYW-AdO5geWAE4wA5gywBtAMwBdDQN0GBpizx4APQ0bb2w8iNu28Y2l24azsaK7p7evv4h7gLBrmboxkwxcaGJRsmWsQEJ0emZPLB4IrouyQwAtAB8DLaZGCASuFa8AA4ALrwA9mCEIAC%2BQA """ qc = QuantumCircuit(3, 1) # Set up the registers if a: qc.x(0) if b: qc.x(1) qc.barrier() # OR # If the first bit is 1, flip the result bit to 1. Otherwise, leave it as 0. qc.cx(0, 2) # If the second bit is 1, flip the result bit again (to 0 if the first is 1, or 1 if the first is 0). Otherwise, leave it as-is. qc.cx(1, 2) # If both bits are 1, flip the result bit one more time. qc.ccx(0, 1, 2) qc.barrier() # Measure qc.measure(2, 0) job = qiskit.execute(qc, qiskit.BasicAer.get_backend('qasm_simulator')) return job.result().get_counts()
 print('XOR') execute(xor) print('AND') execute(and_gate) print('NAND') execute(nand) print('OR') execute(or_gate)
 XOR 0 0: {'0': 1024} 0 1: {'1': 1024} 1 0: {'1': 1024} 1 1: {'0': 1024} AND 0 0: {'0': 1024} 0 1: {'0': 1024} 1 0: {'0': 1024} 1 1: {'1': 1024} NAND 0 0: {'1': 1024} 0 1: {'1': 1024} 1 0: {'1': 1024} 1 1: {'0': 1024} OR 0 0: {'0': 1024} 0 1: {'1': 1024} 1 0: {'1': 1024} 1 1: {'1': 1024}

primaryobjects commented Jul 31, 2021 • edited

 Note, the output bits are read from right to left. That is, the first two right-most bits are the inputs (a, b) and the left-most bit is the output.