From ebfbdf62d288c46d8c39179a9f2d059c21cd42a6 Mon Sep 17 00:00:00 2001 From: Kevin Joven <59969678+KevinJoven11@users.noreply.github.com> Date: Tue, 4 Oct 2022 00:40:32 -0400 Subject: [PATCH 1/8] quantum_teleportation.py This code is for the #Hacktoberfest. This file run the quantum teleportation circuit using Qiskit. --- quantum/quantum_teleportation.py | 60 ++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 quantum/quantum_teleportation.py diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py new file mode 100644 index 000000000000..b77b5e0335c9 --- /dev/null +++ b/quantum/quantum_teleportation.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +""" +Build quantum teleportation circuit using three quantum bits +and 1 classical bit. The main idea is to send one qubit from +Alice to Bob using the entanglement propertie. This experiment +run in IBM Q simulator with 1000 shots. +. +References: +https://en.wikipedia.org/wiki/Quantum_teleportation +https://qiskit.org/textbook/ch-algorithms/teleportation.html +""" + +from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer +import numpy as np + +def quantum_teleportation(theta: float = np.pi/2, phi: float = np.pi/2, lam: float = np.pi/2): + + """ + # >>> quantum_teleportation() + #{'00': 500, '11': 500} # ideally + # ┌─────────────────┐ ┌───┐ + #qr_0: ┤ U(π/2,π/2,π/2) ├───────■──┤ H ├─■───────── + # └──────┬───┬──────┘ ┌─┴─┐└───┘ │ + #qr_1: ───────┤ H ├─────────■──┤ X ├──────┼───■───── + # └───┘ ┌─┴─┐└───┘ │ ┌─┴─┐┌─┐ + #qr_2: ───────────────────┤ X ├───────────■─┤ X ├┤M├ + # └───┘ └───┘└╥┘ + #cr: 1/═══════════════════════════════════════════╩═ + Args: + theta (float): Single qubit rotation U Gate theta parameter. Defaults to np.pi/2 + phi (float): Single qubit rotation U Gate phi parameter. Defaul to np.pi/2 + lam (float): Single qubit rotation U Gate lam parameter. Defaul to np.pi/2 + Returns: + qiskit.result.counts.Counts: Teleported qubit counts. + """ + + qr = QuantumRegister(3,'qr') # Define the number of quantum bits + cr = ClassicalRegister(1,'cr') # Define the number of classical bits + + qc = QuantumCircuit(qr,cr) # Define the quantum circuit. + + # Build the circuit + qc.u(theta, phi, lam,0) # Quantum State to teleport + qc.h(1) + qc.cx(1,2) + qc.cx(0,1) + qc.h(0) + qc.cz(0,2) + qc.cx(1,2) + + qc.measure([2],[0]) + + # Simulate the circuit usong qasm simulator + backend = Aer.get_backend("qasm_simulator") + job = execute(qc, backend, shots=1000) + + return job.result().get_counts(qc) + +if __name__ == "__main__": + print(f"Total count for teleported state is: {quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}") From cfb114c5a1037e06102094ba7b41cd6c7a702eb8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Oct 2022 04:45:02 +0000 Subject: [PATCH 2/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- quantum/quantum_teleportation.py | 36 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index b77b5e0335c9..938247f2e28f 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -10,17 +10,20 @@ https://qiskit.org/textbook/ch-algorithms/teleportation.html """ -from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, execute, Aer import numpy as np +from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute -def quantum_teleportation(theta: float = np.pi/2, phi: float = np.pi/2, lam: float = np.pi/2): + +def quantum_teleportation( + theta: float = np.pi / 2, phi: float = np.pi / 2, lam: float = np.pi / 2 +): """ # >>> quantum_teleportation() #{'00': 500, '11': 500} # ideally - # ┌─────────────────┐ ┌───┐ + # ┌─────────────────┐ ┌───┐ #qr_0: ┤ U(π/2,π/2,π/2) ├───────■──┤ H ├─■───────── - # └──────┬───┬──────┘ ┌─┴─┐└───┘ │ + # └──────┬───┬──────┘ ┌─┴─┐└───┘ │ #qr_1: ───────┤ H ├─────────■──┤ X ├──────┼───■───── # └───┘ ┌─┴─┐└───┘ │ ┌─┴─┐┌─┐ #qr_2: ───────────────────┤ X ├───────────■─┤ X ├┤M├ @@ -33,22 +36,22 @@ def quantum_teleportation(theta: float = np.pi/2, phi: float = np.pi/2, lam: flo Returns: qiskit.result.counts.Counts: Teleported qubit counts. """ - - qr = QuantumRegister(3,'qr') # Define the number of quantum bits - cr = ClassicalRegister(1,'cr') # Define the number of classical bits - qc = QuantumCircuit(qr,cr) # Define the quantum circuit. + qr = QuantumRegister(3, "qr") # Define the number of quantum bits + cr = ClassicalRegister(1, "cr") # Define the number of classical bits + + qc = QuantumCircuit(qr, cr) # Define the quantum circuit. # Build the circuit - qc.u(theta, phi, lam,0) # Quantum State to teleport + qc.u(theta, phi, lam, 0) # Quantum State to teleport qc.h(1) - qc.cx(1,2) - qc.cx(0,1) + qc.cx(1, 2) + qc.cx(0, 1) qc.h(0) - qc.cz(0,2) - qc.cx(1,2) + qc.cz(0, 2) + qc.cx(1, 2) - qc.measure([2],[0]) + qc.measure([2], [0]) # Simulate the circuit usong qasm simulator backend = Aer.get_backend("qasm_simulator") @@ -56,5 +59,8 @@ def quantum_teleportation(theta: float = np.pi/2, phi: float = np.pi/2, lam: flo return job.result().get_counts(qc) + if __name__ == "__main__": - print(f"Total count for teleported state is: {quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}") + print( + f"Total count for teleported state is: {quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}" + ) From 02ccd53c4ed93919581ceeeb7b33085e56d9ab1f Mon Sep 17 00:00:00 2001 From: Kevin Joven <59969678+KevinJoven11@users.noreply.github.com> Date: Tue, 4 Oct 2022 10:34:25 -0400 Subject: [PATCH 3/8] Update quantum/quantum_teleportation.py Co-authored-by: Caeden --- quantum/quantum_teleportation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index 938247f2e28f..a787aa706772 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -53,7 +53,7 @@ def quantum_teleportation( qc.measure([2], [0]) - # Simulate the circuit usong qasm simulator + # Simulate the circuit using qasm simulator backend = Aer.get_backend("qasm_simulator") job = execute(qc, backend, shots=1000) From 0549086f2bd6ca24eb4475b1a0434b1b9d33019c Mon Sep 17 00:00:00 2001 From: Kevin Joven <59969678+KevinJoven11@users.noreply.github.com> Date: Tue, 4 Oct 2022 10:34:40 -0400 Subject: [PATCH 4/8] Update quantum/quantum_teleportation.py Co-authored-by: Caeden --- quantum/quantum_teleportation.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index a787aa706772..bffe786e9006 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -40,18 +40,18 @@ def quantum_teleportation( qr = QuantumRegister(3, "qr") # Define the number of quantum bits cr = ClassicalRegister(1, "cr") # Define the number of classical bits - qc = QuantumCircuit(qr, cr) # Define the quantum circuit. + quantum_circuit = QuantumCircuit(qr, cr) # Define the quantum circuit. # Build the circuit - qc.u(theta, phi, lam, 0) # Quantum State to teleport - qc.h(1) - qc.cx(1, 2) - qc.cx(0, 1) - qc.h(0) - qc.cz(0, 2) - qc.cx(1, 2) + quantum_circuit.u(theta, phi, lam, 0) # Quantum State to teleport + quantum_circuit.h(1) + quantum_circuit.cx(1, 2) + quantum_circuit.cx(0, 1) + quantum_circuit.h(0) + quantum_circuit.cz(0, 2) + quantum_circuit.cx(1, 2) - qc.measure([2], [0]) + quantum_circuit.measure([2], [0]) # Simulate the circuit using qasm simulator backend = Aer.get_backend("qasm_simulator") From d53390fdaf804fe5978283b326f7e707ca4d19be Mon Sep 17 00:00:00 2001 From: Kevin Joven <59969678+KevinJoven11@users.noreply.github.com> Date: Tue, 4 Oct 2022 10:58:54 -0400 Subject: [PATCH 5/8] Update Corrected some typos. Add more comments for adding the gates. Update the variable qc with quantum_circuit in the simulator and execute. --- quantum/quantum_teleportation.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index bffe786e9006..9a0c4313794d 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -2,7 +2,7 @@ """ Build quantum teleportation circuit using three quantum bits and 1 classical bit. The main idea is to send one qubit from -Alice to Bob using the entanglement propertie. This experiment +Alice to Bob using the entanglement properties. This experiment run in IBM Q simulator with 1000 shots. . References: @@ -30,9 +30,9 @@ def quantum_teleportation( # └───┘ └───┘└╥┘ #cr: 1/═══════════════════════════════════════════╩═ Args: - theta (float): Single qubit rotation U Gate theta parameter. Defaults to np.pi/2 - phi (float): Single qubit rotation U Gate phi parameter. Defaul to np.pi/2 - lam (float): Single qubit rotation U Gate lam parameter. Defaul to np.pi/2 + theta (float): Single qubit rotation U Gate theta parameter. Default to np.pi/2 + phi (float): Single qubit rotation U Gate phi parameter. Default to np.pi/2 + lam (float): Single qubit rotation U Gate lam parameter. Default to np.pi/2 Returns: qiskit.result.counts.Counts: Teleported qubit counts. """ @@ -44,21 +44,20 @@ def quantum_teleportation( # Build the circuit quantum_circuit.u(theta, phi, lam, 0) # Quantum State to teleport - quantum_circuit.h(1) - quantum_circuit.cx(1, 2) + quantum_circuit.h(1) # add hadamard gate + quantum_circuit.cx(1, 2) # add control gate with qubit 1 as control and 2 as target. quantum_circuit.cx(0, 1) quantum_circuit.h(0) - quantum_circuit.cz(0, 2) + quantum_circuit.cz(0, 2) # add control z gate. quantum_circuit.cx(1, 2) - quantum_circuit.measure([2], [0]) + quantum_circuit.measure([2], [0]) # measure the qubit. # Simulate the circuit using qasm simulator backend = Aer.get_backend("qasm_simulator") - job = execute(qc, backend, shots=1000) - - return job.result().get_counts(qc) + job = execute(quantum_circuit, backend, shots=1000) + return job.result().get_counts(quantum_circuit) if __name__ == "__main__": print( From 63241c54f42b7a85a71c2594c6a747bbedbcd815 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Oct 2022 14:59:48 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- quantum/quantum_teleportation.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index 9a0c4313794d..ec374aeaf96a 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -44,14 +44,16 @@ def quantum_teleportation( # Build the circuit quantum_circuit.u(theta, phi, lam, 0) # Quantum State to teleport - quantum_circuit.h(1) # add hadamard gate - quantum_circuit.cx(1, 2) # add control gate with qubit 1 as control and 2 as target. + quantum_circuit.h(1) # add hadamard gate + quantum_circuit.cx( + 1, 2 + ) # add control gate with qubit 1 as control and 2 as target. quantum_circuit.cx(0, 1) quantum_circuit.h(0) - quantum_circuit.cz(0, 2) # add control z gate. + quantum_circuit.cz(0, 2) # add control z gate. quantum_circuit.cx(1, 2) - quantum_circuit.measure([2], [0]) # measure the qubit. + quantum_circuit.measure([2], [0]) # measure the qubit. # Simulate the circuit using qasm simulator backend = Aer.get_backend("qasm_simulator") @@ -59,6 +61,7 @@ def quantum_teleportation( return job.result().get_counts(quantum_circuit) + if __name__ == "__main__": print( f"Total count for teleported state is: {quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}" From d805320a6e93ddcfd27b80316621a51865bb65bb Mon Sep 17 00:00:00 2001 From: Kevin Joven <59969678+KevinJoven11@users.noreply.github.com> Date: Tue, 4 Oct 2022 11:04:25 -0400 Subject: [PATCH 7/8] python return typehint solved. --- quantum/quantum_teleportation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index ec374aeaf96a..ac2480293f6f 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -11,12 +11,13 @@ """ import numpy as np +import qiskit from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute def quantum_teleportation( theta: float = np.pi / 2, phi: float = np.pi / 2, lam: float = np.pi / 2 -): +) -> qiskit.result.counts.Counts: """ # >>> quantum_teleportation() From 5430f87dc80940ef52e6ceba2bd79f8dc07ece9c Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 4 Oct 2022 22:44:35 +0200 Subject: [PATCH 8/8] Fix long line --- quantum/quantum_teleportation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quantum/quantum_teleportation.py b/quantum/quantum_teleportation.py index ac2480293f6f..5fbc57a66821 100644 --- a/quantum/quantum_teleportation.py +++ b/quantum/quantum_teleportation.py @@ -65,5 +65,6 @@ def quantum_teleportation( if __name__ == "__main__": print( - f"Total count for teleported state is: {quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}" + "Total count for teleported state is: " + f"{quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}" )