1) Teleportation 2) Superdense coding, 3) Deutsch-Jozsa Algorithm 4) Grover's Algo 5)…
- User
- Teleportation 2) Superdense coding, 3) Deutsch-Jozsa Algorithm 4) Grover's Algo 5) BB84
Qiskit codes and syntax
- Assistant
Certainly! Below are simple Qiskit code snippets and explanations for each of the listed quantum protocols and algorithms:
1) Quantum Teleportation
Teleport an unknown qubit state from Alice to Bob using an entangled pair.
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, Aer, execute # Create quantum and classical registers q = QuantumRegister(3, 'q') c = ClassicalRegister(2, 'c') qc = QuantumCircuit(q, c) # Prepare the state to teleport on q[0], for example |+> state qc.h(q[0]) # This is the state to be teleported # Create entanglement between q[1] and q[2] qc.h(q[1]) qc.cx(q[1], q[2]) # Bell measurement on q[0] and q[1] qc.cx(q[0], q[1]) qc.h(q[0]) qc.measure(q[0], c[0]) qc.measure(q[1], c[1]) # Conditional operations on q[2] based on measurement results qc.cx(q[1], q[2]).c_if(c, 1) qc.cz(q[0], q[2]).c_if(c, 2) # Note: Qiskit conditional gates require special syntax below # Actually we need to do correct classical conditionals manually: # So do the corrections after measurement results are read out in simulation # Execute the circuit backend = Aer.get_backend('qasm_simulator') shots = 1024 job = execute(qc, backend, shots=shots) result = job.result() counts = result.get_counts() print(counts) # For visualizing the circuit: print(qc.draw())Note: Qiskit's standard conditional
c_ifworks only on single classical bits, to do the classical conditional corrections based on two classical bits stored inc[0]andc[1], a common approach is to split them into two classical bits and apply gates conditionally after measurement, or useqiskit.quantum_infoclasses or classical post-processing. For teleportation demonstration, it's usually better to simulate statevector circuit and manually apply the corrections.
2) Superdense Coding
Send two classical bits of information using only one qubit and an entangled pair.
from qiskit import QuantumCircuit, Aer, execute # Create circuit with 2 qubits and 2 classical bits to retrieve results qc = QuantumCircuit(2, 2) # Create entangled pair qc.h(0) qc.cx(0, 1) # Encode classical bits into qubit 0 # Let's say we want to send '11' qc.z(0) qc.x(0) # Decode the bits at the receiver side qc.cx(0, 1) qc.h(0) # Measure both qubits to get the two bits qc.measure([0, 1], [0, 1]) # Execute the circuit backend = Aer.get_backend('qasm_simulator') job = execute(qc, backend, shots=1024) result = job.result() counts = result.get_counts() print(counts) # Check circuit print(qc.draw())
3) Deutsch-Jozsa Algorithm
Distinguish between constant and balanced functions with a single query.
from qiskit import QuantumCircuit, Aer, execute from qiskit.quantum_info import Statevector n = 3 # Number of input bits # Create quantum circuit with n input qubits and 1 output qubit qc = QuantumCircuit(n+1, n) # Initialize output qubit in |-> state qc.x(n) qc.h(n) # Apply Hadamard on input qubits for i in range(n): qc.h(i) # Oracle for a balanced function (example): flip output qubit if input qubit 0 is 1 # For Deutsch-Jozsa algorithm, you define your oracle here qc.cx(0, n) # balanced oracle example # Apply Hadamard on input qubits again for i in range(n): qc.h(i) # Measure input qubits qc.measure(range(n), range(n)) # Execute circuit backend = Aer.get_backend('qasm_simulator') job = execute(qc, backend, shots=1024) counts = job.result().get_counts() print(counts) print(qc.draw())Interpretation:
- If the output is
0...0(all zero), function is constant. - Otherwise, it's balanced.
4) Grover's Algorithm
Search for a marked item in an unsorted database.
from qiskit import QuantumCircuit, Aer, execute from qiskit.circuit.library import GroverOperator from qiskit.algorithms import AmplificationProblem, Grover # Define number of qubits n = 2 # Define the oracle that marks the state '11' oracle = QuantumCircuit(n) oracle.cz(0, 1) # Phase flip for |11> # Create Grover problem problem = AmplificationProblem(oracle) # Initialize Grover grover = Grover(quantum_instance=Aer.get_backend('qasm_simulator')) result = grover.amplify(problem) print("Top result:", result.top_measurement) # Alternatively build Grover circuit manuallyAlternatively, a full manual Grover iteration:
qc = QuantumCircuit(n, n) qc.h(range(n)) # Initialize superposition # Oracle: phase inversion for |11> qc.cz(0, 1) # Diffuser (inversion about mean) qc.h(range(n)) qc.x(range(n)) qc.cz(0, 1) qc.x(range(n)) qc.h(range(n)) qc.measure(range(n), range(n)) backend = Aer.get_backend('qasm_simulator') job = execute(qc, backend, shots=1024) counts = job.result().get_counts() print(counts) print(qc.draw())
5) BB84 Protocol (Basic Simulation)
Simulate BB84 key distribution between Alice and Bob:
import random from qiskit import QuantumCircuit, Aer, execute n = 10 # Alice chooses random bits and bases alice_bits = [random.randint(0,1) for _ in range(n)] alice_bases = [random.choice(['Z','X']) for _ in range(n)] # Bob chooses random bases bob_bases = [random.choice(['Z','X']) for _ in range(n)] key = [] backend = Aer.get_backend('qasm_simulator') for i in range(n): qc = QuantumCircuit(1,1) # Prepare qubit in the state depending on Alice's bit and basis if alice_bases[i] == 'Z': if alice_bits[i] == 1: qc.x(0) else: # X basis if alice_bits[i] == 0: qc.h(0) else: qc.x(0) qc.h(0) # Bob measurement basis if bob_bases[i] == 'X': qc.h(0) qc.measure(0,0) result = execute(qc, backend, shots=1).result() meas = int(result.get_counts().most_frequent()) # If bases match, add the bit to key if alice_bases[i] == bob_bases[i]: key.append(meas) print(f"Alice bits: {alice_bits}") print(f"Alice bases: {alice_bases}") print(f"Bob bases: {bob_bases}") print(f"Shared key: {key}")
If you want me to generate more detailed full codes or visualizations for any of these, just ask!
- If the output is