Loading notebooks/Half-Adder-PL.ipynb 0 → 100644 +136 −0 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python import pennylane as qml from pennylane import numpy as np ``` %% Cell type:code id: tags: ``` python dev1 = qml.device('default.qubit.autograd', wires = 4, shots =100000) @qml.qnode(dev1) def circuit(params): if params[0] == 1: qml.PauliX(wires=0) if params[1] == 1: qml.PauliX(wires=1) #return qml.probs(wires=[0,1]) #return qml.probs(wires=1) qml.CNOT(wires=[0,2]) qml.CNOT(wires=[1,2]) qml.Toffoli(wires=[0,1,3]) #return qml.state() #return qml.sample(qml.PauliZ(2)) #return qml.expval(qml.PauliZ(2)) return qml.probs(wires=[2,3]) #drawer = qml.draw(circuit) p = [0,1] print(circuit(p)) ``` %% Output [0. 0. 1. 0.] %% Cell type:code id: tags: ``` python a = [1,1] probs = circuit(a) print(probs) if (probs[0] == 1) carry = 0; sum = 0 #state = |00> if (probs[1] == 1) carry = 0; sum = 0 print("Carry Probabilities:", probs[:2] ) ``` %% Output [0. 1. 0. 0.] Carry Probabilities: [0. 1.] %% Cell type:code id: tags: ``` python drawer = qml.draw(circuit) print(drawer(p)) ``` %% Output 0: ──X─╭●────╭●─┤ 1: ────│──╭●─├●─┤ 2: ────╰X─╰X─│──┤ ╭Probs 3: ──────────╰X─┤ ╰Probs %% Cell type:code id: tags: ``` python dev2 = qml.device("default.qubit", wires=2) @qml.qnode(dev2) def apply_h_cnot(): # APPLY THE OPERATIONS IN THE CIRCUIT qml.Hadamard(wires=0) qml.CNOT(wires=[1,0]) return qml.state() print(apply_h_cnot()) ``` %% Output [0.70710678+0.j 0. +0.j 0.70710678+0.j 0. +0.j] %% Cell type:code id: tags: ``` python #import sys #!conda install --yes --prefix {sys.prefix} pylatexenc from qiskit import * from qiskit.tools.visualization import plot_bloch_multivector from qiskit.tools.visualization import plot_histogram # Creating a circuit with 2 quantum bits and one classical bit qc = QuantumCircuit(2,1) # Preparing inputs qc.x(0) # Comment this line to make Qbit0 = |0> qc.x(1) # Comment this line to make Qbit1 = |0> qc.barrier() # Applying the CNOT gate qc.cx(0,1) qc.barrier() # Measuring Qbit1 and put result to classical bit qc.measure(1,0) # Drawing the circuit diagram #qc.draw(output='mpl') # Run the experimient 1024 times and get stats counts = execute(qc,Aer.get_backend('qasm_simulator')).result().get_counts() plot_histogram(counts) ``` %% Output <Figure size 700x500 with 1 Axes> Loading
notebooks/Half-Adder-PL.ipynb 0 → 100644 +136 −0 Original line number Diff line number Diff line %% Cell type:code id: tags: ``` python import pennylane as qml from pennylane import numpy as np ``` %% Cell type:code id: tags: ``` python dev1 = qml.device('default.qubit.autograd', wires = 4, shots =100000) @qml.qnode(dev1) def circuit(params): if params[0] == 1: qml.PauliX(wires=0) if params[1] == 1: qml.PauliX(wires=1) #return qml.probs(wires=[0,1]) #return qml.probs(wires=1) qml.CNOT(wires=[0,2]) qml.CNOT(wires=[1,2]) qml.Toffoli(wires=[0,1,3]) #return qml.state() #return qml.sample(qml.PauliZ(2)) #return qml.expval(qml.PauliZ(2)) return qml.probs(wires=[2,3]) #drawer = qml.draw(circuit) p = [0,1] print(circuit(p)) ``` %% Output [0. 0. 1. 0.] %% Cell type:code id: tags: ``` python a = [1,1] probs = circuit(a) print(probs) if (probs[0] == 1) carry = 0; sum = 0 #state = |00> if (probs[1] == 1) carry = 0; sum = 0 print("Carry Probabilities:", probs[:2] ) ``` %% Output [0. 1. 0. 0.] Carry Probabilities: [0. 1.] %% Cell type:code id: tags: ``` python drawer = qml.draw(circuit) print(drawer(p)) ``` %% Output 0: ──X─╭●────╭●─┤ 1: ────│──╭●─├●─┤ 2: ────╰X─╰X─│──┤ ╭Probs 3: ──────────╰X─┤ ╰Probs %% Cell type:code id: tags: ``` python dev2 = qml.device("default.qubit", wires=2) @qml.qnode(dev2) def apply_h_cnot(): # APPLY THE OPERATIONS IN THE CIRCUIT qml.Hadamard(wires=0) qml.CNOT(wires=[1,0]) return qml.state() print(apply_h_cnot()) ``` %% Output [0.70710678+0.j 0. +0.j 0.70710678+0.j 0. +0.j] %% Cell type:code id: tags: ``` python #import sys #!conda install --yes --prefix {sys.prefix} pylatexenc from qiskit import * from qiskit.tools.visualization import plot_bloch_multivector from qiskit.tools.visualization import plot_histogram # Creating a circuit with 2 quantum bits and one classical bit qc = QuantumCircuit(2,1) # Preparing inputs qc.x(0) # Comment this line to make Qbit0 = |0> qc.x(1) # Comment this line to make Qbit1 = |0> qc.barrier() # Applying the CNOT gate qc.cx(0,1) qc.barrier() # Measuring Qbit1 and put result to classical bit qc.measure(1,0) # Drawing the circuit diagram #qc.draw(output='mpl') # Run the experimient 1024 times and get stats counts = execute(qc,Aer.get_backend('qasm_simulator')).result().get_counts() plot_histogram(counts) ``` %% Output <Figure size 700x500 with 1 Axes>