pip install qiskit

!pip install pylatexenc

pip install qiskit-aer

pip install git+https://github.com/Qiskit/qiskit-terra.git

import numpy as np

# Importing standard Qiskit libraries
from qiskit import QuantumCircuit, transpile, assemble, Aer, IBMQ, execute
from qiskit.quantum_info import Statevector
from qiskit.visualization import plot_bloch_multivector, plot_histogram, plot_histogram, plot_state_city

Aer.backends()

# Construct quantum circuit without measure
circ = QuantumCircuit(2,2)
circ.h(0)
circ.cx(0, 1) #CNOT gate
circ.measure([0,1], [0,1])
circ.draw('mpl')

# Select the QasmSimulator from the Aer provider
simulator = Aer.get_backend('qasm_simulator')

# Execute and get memory
result = execute(circ, simulator, shots=100, memory=True).result()
memory = result.get_memory(circ)
print(memory)

# Select the StatevectorSimulator from the Aer provider
simulator = Aer.get_backend('statevector_simulator')

# Execute and get counts
result = execute(circ, simulator).result()
statevector = result.get_statevector(circ)
plot_state_city(statevector, title='Bell state')

def psiplus():
    qc = QuantumCircuit(2,2) # this time, we not only want two qubits, but also two classical bits for the measurement
    qc.h(0)
    qc.x(1)
    qc.cx(0,1)
    return qc

qc = psiplus()
qc.draw('mpl') # we draw the circuit

qc.measure_all() # we measure all the qubits
backend = Aer.get_backend('qasm_simulator') # we choose the simulator as our backend
counts = execute(qc, backend, shots = 1000).result().get_counts() # we run the simulation and get the counts
plot_histogram(counts) # let us plot a histogram to see the possible outcomes and corresponding probabilities

#lets first construct the circuit for the GHZ state

def GHZ():
    circ = QuantumCircuit(3,3)
    circ.h(0)
    circ.cx(0,1)
    circ.cx(1,2)
    return circ


circ = GHZ()
circ.draw('mpl') # draw the circuit

circ.measure_all() # measure all the qubits
backend = Aer.get_backend('qasm_simulator') # we choose the simulator as our backend
counts = execute(circ, backend, shots = 2000).result().get_counts() # we run the simulation and get the counts
plot_histogram(counts) # plot a histogram to see the possible outcomes and corresponding probabilities

def GHZlike():
    circ = QuantumCircuit(3,3)
    circ.h(0)
    circ.z(0)
    circ.x(1)
    circ.x(2)
    circ.cx(0,1)
    circ.cx(1,2)
    return circ


circ = GHZlike()
circ.draw('mpl') # draw the circuit

circ.measure_all() # measure all the qubits
backend = Aer.get_backend('qasm_simulator') # we choose the simulator as our backend
counts = execute(circ, backend, shots = 2000).result().get_counts() # we run the simulation and get the counts
plot_histogram(counts) # plot a histogram to see the possible outcomes and corresponding probabilities

def GHZlike():
    circ = QuantumCircuit(3,3)
    circ.h(0)
    circ.z(0)
    circ.x(1)
    circ.x(2)
    circ.cx(0,1)
    circ.cx(1,2)
    return circ


qc = GHZlike()
qc.draw('mpl') # draw the circuit

# Execute and get counts
result = execute(qc, simulator).result()
statevector = result.get_statevector(qc)
plot_state_city(statevector, title='GHZ-like state')

# Construct an empty quantum circuit which makes the first bell state
qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)

# Select the UnitarySimulator from the Aer provider
simulator = Aer.get_backend('unitary_simulator')

# Execute and get counts
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
print("Circuit unitary:\n", unitary)

