๐Ÿ“ฆ TheAlgorithms / Python

๐Ÿ“„ quantum_teleportation.py.DISABLED.txt ยท 70 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#!/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 properties. 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
"""

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()
    #{'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. 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.
    """

    qr = QuantumRegister(3, "qr")  # Define the number of quantum bits
    cr = ClassicalRegister(1, "cr")  # Define the number of classical bits

    quantum_circuit = QuantumCircuit(qr, cr)  # Define the quantum circuit.

    # 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.cx(0, 1)
    quantum_circuit.h(0)
    quantum_circuit.cz(0, 2)  # add control z gate.
    quantum_circuit.cx(1, 2)

    quantum_circuit.measure([2], [0])  # measure the qubit.

    # Simulate the circuit using qasm simulator
    backend = Aer.get_backend("aer_simulator")
    job = execute(quantum_circuit, backend, shots=1000)

    return job.result().get_counts(quantum_circuit)


if __name__ == "__main__":
    print(
        "Total count for teleported state is: "
        f"{quantum_teleportation(np.pi/2, np.pi/2, np.pi/2)}"
    )