From 222fa19506c7315309c87565f4f1bc019c578abd Mon Sep 17 00:00:00 2001 From: Abhishek Jaisingh Date: Tue, 13 Oct 2020 17:28:33 +0530 Subject: [PATCH 1/2] Add Qiskit Quantum NOT Gate Example Code --- quantum/single_qubit_not_gate.py | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 quantum/single_qubit_not_gate.py diff --git a/quantum/single_qubit_not_gate.py b/quantum/single_qubit_not_gate.py new file mode 100644 index 000000000000..ee5e899a02aa --- /dev/null +++ b/quantum/single_qubit_not_gate.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 +""" +Build a simple bare-minimum quantum circuit that starts with a single +qubit (by default, in state 0) and inverts it. Run the experiment 1000 +times and print the total count of the states finally observed. +Qiskit Docs: https://qiskit.org/documentation/getting_started.html +""" + + +import qiskit as q + + +def single_qubit_measure() -> q.result.counts.Counts: + """ + >>> single_qubit_measure() + {'1': 1000} + """ + # Use Aer's qasm_simulator + simulator = q.Aer.get_backend("qasm_simulator") + + # Create a Quantum Circuit acting on the q register + circuit = q.QuantumCircuit(1, 1) + + # Apply X (NOT) Gate to Qubit 0 + circuit.x(0) + + # Map the quantum measurement to the classical bits + circuit.measure([0], [0]) + + # Execute the circuit on the qasm simulator + job = q.execute(circuit, simulator, shots=1000) + + # Grab results from the job + result = job.result() + + # Get the histogram data of an experiment. + counts = result.get_counts(circuit) + + return counts + + +if __name__ == "__main__": + counts = single_qubit_measure() + print("Total count for various states are:", counts) From 78c281bb544ca69555f852672d9d1f83a884abcb Mon Sep 17 00:00:00 2001 From: Abhishek Jaisingh Date: Tue, 13 Oct 2020 21:44:51 +0530 Subject: [PATCH 2/2] Address Review Comments Signed-off-by: Abhishek Jaisingh --- .../{single_qubit_not_gate.py => not_gate.py} | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) rename quantum/{single_qubit_not_gate.py => not_gate.py} (51%) diff --git a/quantum/single_qubit_not_gate.py b/quantum/not_gate.py similarity index 51% rename from quantum/single_qubit_not_gate.py rename to quantum/not_gate.py index ee5e899a02aa..4f9fa1319ac9 100644 --- a/quantum/single_qubit_not_gate.py +++ b/quantum/not_gate.py @@ -6,39 +6,34 @@ Qiskit Docs: https://qiskit.org/documentation/getting_started.html """ - import qiskit as q -def single_qubit_measure() -> q.result.counts.Counts: +def single_qubit_measure(qubits: int, classical_bits: int) -> q.result.counts.Counts: """ - >>> single_qubit_measure() - {'1': 1000} + >>> single_qubit_measure(1, 1) + {'11': 1000} """ # Use Aer's qasm_simulator - simulator = q.Aer.get_backend("qasm_simulator") + simulator = q.Aer.get_backend('qasm_simulator') # Create a Quantum Circuit acting on the q register - circuit = q.QuantumCircuit(1, 1) + circuit = q.QuantumCircuit(qubits, classical_bits) - # Apply X (NOT) Gate to Qubit 0 + # Apply X (NOT) Gate to Qubits 0 & 1 circuit.x(0) + circuit.x(1) # Map the quantum measurement to the classical bits - circuit.measure([0], [0]) + circuit.measure([0, 1], [0, 1]) # Execute the circuit on the qasm simulator job = q.execute(circuit, simulator, shots=1000) - # Grab results from the job - result = job.result() - - # Get the histogram data of an experiment. - counts = result.get_counts(circuit) - - return counts + # Return the histogram data of the results of the experiment. + return job.result().get_counts(circuit) -if __name__ == "__main__": - counts = single_qubit_measure() - print("Total count for various states are:", counts) +if __name__ == '__main__': + counts = single_qubit_measure(2, 2) + print(f'Total count for various states are: {counts}')