<jupyter_start><jupyter_text>Qubit by QubitIntroduction to Quantum ComputingWeek 16 - Review WeekLab notebook<jupyter_code>import numpy as np
# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, execute, transpile, Aer, IBMQ
from qiskit.tools.jupyter import *
from qiskit.visualization import *
from ibm_quantum_widgets import *
from qiskit.quantum_info import random_unitary

# Loading your IBM Quantum account(s)
provider = IBMQ.load_account()
print('Libraries imported successfully!')<jupyter_output>Libraries imported successfully!<jupyter_text>Coding cheat sheet:`qc = QuantumCircuit(2)`  Define a 2 qubit quantum circuit `qc = QuantumCircuit(2,2)`  Define a 2 qubit quantum circuit with 2 classical bit (Only for QASM simulator or real Quantum Computer) `qc.x(0)` Add an X gate `qc.h(0)` Add an H gate `qc.z(0)` Add a Z gate `qc.measure([0,1],[0,1])`  Add measurement to circuit (Only for QASM simulator or real Quantum Computer) `qc.draw()`  Draw the circuit  Set up and run the statevector simulator:`backend = Aer.get_backend('statevector_simulator')`  Tell it which simulator you want to use `job = execute(qc,backend)`  Put in the name of your quantum circuit where it says qc`result = job.result()`  Set up and run the QASM simulator:`backend = Aer.get_backend('qasm_simulator')`  Tell it which simulator you want to use `job = execute(qc,backend, shots = 1024)`  Put in the name of your quantum circuit where it says qc, and the number of shots you want to use`result = job.result()`  Set up and run on a real quantum computer :`from qiskit.providers.ibmq import least_busy` `backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 and`                                         `not x.configuration().simulator and x.status().operational==True))`  Find the least busy QC `job = execute(qc,backend, shots = 1024)`  Put in the name of your quantum circuit where it says qc, and the number of shots you want to use`result = job.result()`  See the output in vector form (only for Statevector simulator):`state = result.get_statevector()` `array_to_latex(state, prefix="\\text{Statevector} = ")`  See the output in histogram form (for Statevector, QASM, or real quantum computer): `counts = result.get_counts()` `plot_histogram(counts)`<jupyter_code># Block 1: Look at the code in this block. Can you describe the circuit that it creates?
qc = QuantumCircuit(2,2)
qc.h(0)
qc.z(0)
qc.cx(0,1)
qc.measure([0,1],[0,1])
qc.draw()
#Block 2: Look at the code in this block.
#Which simulator is being used to run the circuit? 
#How many times will the circuit be simulated?
#Is the output being presented as a histogram, or as a vector?


backend = Aer.get_backend('qasm_simulator')
job = execute(qc,backend, shots = 1024)
result = job.result()

counts = result.get_counts()
plot_histogram(counts)
# Block 3: Now, we want to run the same circuit, but we want to use Statevector simulator. 
# What changes would we have to make to the circuit, as defined in the code from Block 1? 
# Copy the code from Block 1 and make those changes.
qc = QuantumCircuit(2)
qc.h(0)
qc.z(0)
qc.cx(0,1)
qc.draw()
# Block 4: What changes do we have to make to the code from Block 2 for running the circuit? 
# Copy the code from that block here and make those changes. 
# Do you expect the results to look different?
backend = Aer.get_backend('statevector_simulator') # Tell it which simulator you want to use
job = execute(qc,backend) # Put in the name of your quantum circuit where it says qc
result = job.result()

counts = result.get_counts()
plot_histogram(counts)
# Optional Block 1: Create a random unitary of size 4 using the random_unitary function. 
# Print U to see what it looks like.
# Syntax: U = random_unitary(size)

U = random_unitary(4)
print(U)
# Optional Block 2: Create a 2 qubit quantum circuit named qc. 
# Apply the transformation U to the 2 qubits in the circuit. Draw the circuit.
# Syntax: qc.unitary(U,[qubit_indices])

qc = QuantumCircuit(2)
qc.unitary(U,[0,1])
qc.draw()
# Optional Block 3: Simulate this circuit using statevector simulator to get the final histogram. 
# What probabilities do you get for the different output states?
backend = Aer.get_backend('statevector_simulator') # Tell it which simulator you want to use
job = execute(qc,backend) # Put in the name of your quantum circuit where it says qc
result = job.result()

counts = result.get_counts()
plot_histogram(counts)
# Optional Block 4: Run this block to transpile the circuit qc into a new circuit transpiled_qc. 
# This circuit achieves the same transformation using the gates Rx, Ry, and CX (specified by the basis_gates parameter in the transpile() function)
# Draw the transpiled circuit

transpiled_qc = transpile(qc,basis_gates = ['rx','ry','cx'])
transpiled_qc.draw()
# Optional Block 5: Simulate the transpiled circuit transpiled_qc using statevector simulator to get the final histogram. 
# Do you get the same probabilities for the output states as the original circuit qc?

backend = Aer.get_backend('statevector_simulator') # Tell it which simulator you want to use
job = execute(transpiled_qc,backend) # Put in the name of your quantum circuit where it says qc
result = job.result()

counts = result.get_counts()
plot_histogram(counts)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08716114457831325, 'fraction_numerical': 0.011671686746987951, 'mean_word_length': 4.949608062709967, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '320415', 'license': 'MIT', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1567, 'n_words': 676, 'path': 'Week 16 Lab.ipynb', 'repo_name': 'KenWuqianghao/QxQ-Intro-to-Quantum-Computing', 'size': '5312'}