## Blank Code Cell
## Use only if you need to install the grader and/or Qiskit
## If you are running this notebook in the IBM Quantum Lab - you can ignore this cell

!pip install qiskit
!pip install 'qc-grader[qiskit] @ git+https://github.com/qiskit-community/Quantum-Challenge-Grader.git'

## Run this cell to make sure your grader is setup correctly

%set_env QC_GRADE_ONLY=true
%set_env QC_GRADING_ENDPOINT=https://qac-grading.quantum-computing.ibm.com

from qiskit import QuantumCircuit

qc = QuantumCircuit(3, 3)


## Write your code below this line ##




## Do not change the code below here ##

answer1 = qc
qc.draw()

# Grader Cell: Run this to submit your answer
from qc_grader.challenges.fall_fest23 import grade_ex2a

grade_ex2a(answer1)

from qiskit import QuantumCircuit

qc = QuantumCircuit(3, 3)

qc.barrier(0, 1, 2)


## Write your code below this line ##




## Do not change the code below this line ##


answer2 = qc
qc.draw()

# Grader Cell: Run this to submit your answer

from qc_grader.challenges.fall_fest23 import grade_ex2b

grade_ex2b(answer2)

answer3: bool

## Quiz: evaluate the results and decide if the following statement is True or False

q0 = 1
q1 = 0
q2 = 1

## Based on this, is it TRUE or FALSE that the Guard on the left is a liar?
## Assign your answer, either True or False, to answer3 below

answer3 =

from qc_grader.challenges.fall_fest23 import grade_ex2c

grade_ex2c(answer3)

## Quiz: Fill in the correct numbers to make the following statement true:
## The treasure is on the right, and the Guard on the left is the liar

q0 =
q1 =
q2 =

## HINT - Remember that Qiskit uses little-endian ordering

answer4 = [q0, q1, q2]

# Grader Cell: Run this to submit your answer

from qc_grader.challenges.fall_fest23 import grade_ex2d

grade_ex2d(answer4)

from qiskit import QuantumCircuit

qc = QuantumCircuit(3)


## in the code below, fill in the missing gates. Run the cell to see a drawing of the current circuit ##


qc.h(0)

qc.barrier(0, 1, 2)

qc.x(2)

qc.cx(2, 0)

qc.barrier(0, 1, 2)

qc.cx(2, 1)

qc.x(0)


## Do not change any of the code below this line ##


answer5 = qc
qc.draw(output="mpl")

# Grader Cell: Run this to submit your answer

from qc_grader.challenges.fall_fest23 import grade_ex2e

grade_ex2e(answer5)

from qiskit import QuantumCircuit, Aer, transpile
from qiskit.visualization import plot_histogram


## This is the full version of the circuit. Run it to see the results ##

quantCirc = QuantumCircuit(3)

quantCirc.h(0), quantCirc.h(2), quantCirc.cx(0, 1), quantCirc.barrier(0, 1, 2), quantCirc.cx(2, 1), quantCirc.x(2), quantCirc.cx(2, 0), quantCirc.x(2)
quantCirc.barrier(0, 1, 2), quantCirc.swap(0, 1), quantCirc.x(1), quantCirc.cx(2, 1), quantCirc.x(0), quantCirc.x(2), quantCirc.cx(2, 0), quantCirc.x(2)

# Execute the circuit and draw the histogram

measured_qc = quantCirc.measure_all(inplace=False)
backend = Aer.get_backend('qasm_simulator') # the device to run on
result = backend.run(transpile(measured_qc, backend), shots=1000).result()
counts  = result.get_counts(measured_qc)
plot_histogram(counts)


from qiskit.primitives import Sampler
from qiskit.visualization import plot_distribution

sampler = Sampler()


result = sampler.run(measured_qc, shots=1000).result()

probs = result.quasi_dists[0].binary_probabilities()
plot_distribution(probs)