|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Build a quantum circuit with pair or group of qubits to perform |
| 4 | +quantum entanglement. |
| 5 | +Quantum entanglement is a phenomenon observed at the quantum scale |
| 6 | +where entangled particles stay connected (in some sense) so that |
| 7 | +the actions performed on one of the particles affects the other, |
| 8 | +no matter the distance between two particles. |
| 9 | +""" |
| 10 | + |
| 11 | +import qiskit |
| 12 | + |
| 13 | + |
| 14 | +def quantum_entanglement(qubits: int = 2) -> qiskit.result.counts.Counts: |
| 15 | + """ |
| 16 | + # >>> quantum_entanglement(2) |
| 17 | + # {'00': 500, '11': 500} |
| 18 | + # ┌───┐ ┌─┐ |
| 19 | + # q_0: ┤ H ├──■──┤M├─── |
| 20 | + # └───┘┌─┴─┐└╥┘┌─┐ |
| 21 | + # q_1: ─────┤ X ├─╫─┤M├ |
| 22 | + # └───┘ ║ └╥┘ |
| 23 | + # c: 2/═══════════╩══╩═ |
| 24 | + # 0 1 |
| 25 | + Args: |
| 26 | + qubits (int): number of quibits to use. Defaults to 2 |
| 27 | + Returns: |
| 28 | + qiskit.result.counts.Counts: mapping of states to its counts |
| 29 | + """ |
| 30 | + classical_bits = qubits |
| 31 | + |
| 32 | + # Using Aer's qasm_simulator |
| 33 | + simulator = qiskit.Aer.get_backend("qasm_simulator") |
| 34 | + |
| 35 | + # Creating a Quantum Circuit acting on the q register |
| 36 | + circuit = qiskit.QuantumCircuit(qubits, classical_bits) |
| 37 | + |
| 38 | + # Adding a H gate on qubit 0 (now q0 in superposition) |
| 39 | + circuit.h(0) |
| 40 | + |
| 41 | + for i in range(1, qubits): |
| 42 | + # Adding CX (CNOT) gate |
| 43 | + circuit.cx(i - 1, i) |
| 44 | + |
| 45 | + # Mapping the quantum measurement to the classical bits |
| 46 | + circuit.measure(list(range(qubits)), list(range(classical_bits))) |
| 47 | + |
| 48 | + # Now measuring any one qubit would affect other qubits to collapse |
| 49 | + # their super position and have same state as the measured one. |
| 50 | + |
| 51 | + # Executing the circuit on the qasm simulator |
| 52 | + job = qiskit.execute(circuit, simulator, shots=1000) |
| 53 | + |
| 54 | + return job.result().get_counts(circuit) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + print(f"Total count for various states are: {quantum_entanglement(3)}") |
0 commit comments