1
$\begingroup$

I'm attempting to work through the exercise at the end of https://learn.qiskit.org/course/ch-gates/phase-kickback#phase-8-0 where I am to build and implement Deutsch's algorithm. I understand that deutsch_problem(seed=None) will randomly determine which function is in the black box, but I am not sure how to implement it using Python coding in the def deutsch(function).

My attempt is below and though I have not yet run this on qasm_simulator to see what result I get, I feel that since qc.draw() only gives me the circuit leading up to the black-box, something must be incorrect.

My attempt


    # My code within def deutsch(function):
    deutsch_problem(seed=None)    #I THINK this randomly decides which of the 4 functions are in the black box
   
    #The next three lines are my attempt to turn the random function into a unitary for the circuit
    usim = Aer.get_backend('unitary_simulator')
    qobj = assemble(problem)  
    unitary = usim.run(qobj).result().get_unitary()
    
    #The next section builds Deutsch's algorithm to measure the outcome
    qc=QuantumCircuit(2,1)
    qc.x(1)
    qc.h([0,1])
    qc.compose(unitary)
    qc.h(0)
    qc.measure(0,0)
$\endgroup$

1 Answer 1

1
$\begingroup$

This is how I do it, I tried to append gate directly with to_gate, remember to transpile or decompose the label gate before run simulator. if you want to get unitary of the gate, you can use Operator directly


def deutsch(function):
    """Implements Deutsch's algorithm.

    Args:
        function (QuantumCircuit): Deutsch function to solve.
            Must be a 2-qubit circuit, and either balanced,
            or constant.
    Returns:
        bool: True if the circuit is balanced, otherwise False.
    """

    # your code hereqc=QuantumCircuit(2,1)
    qc = QuantumCircuit(2,1)
    qc.x(1)
    qc.h([0,1])
    qc.barrier()
    ################## Three way to append the deutsch_problem()
    qc.append(problem.to_gate(label='unitary'),[0,1])
    #qc.append(Operator(problem),[0,1])
    #qc.unitary(Operator(problem),[0,1])
    ###################
    qc.barrier()
    qc.h(0)
    qc.measure(0,0)
    qc = qc.decompose('unitary')
    display(qc.draw())
    svsim = Aer.get_backend('aer_simulator')
    #qc = transpile(qc,svsim)
    final_state = svsim.run(qc).result().get_counts()#.get_statevector()
    final_state = list(final_state.keys())[0]
    if final_state == '0':
      return 'constant'
    if final_state == '1':
      return 'balance'
from qiskit import Aer, assemble
from qiskit.quantum_info.operators import Operator
problem =deutsch_problem()
#display(problem.draw())
deutsch(problem)
    
```
$\endgroup$
2
  • $\begingroup$ Thank you so much! Much of python is still new and I had not yet seen append, to_gate, Operator(), ... so this has taught me more than I had hoped! $\endgroup$
    – PGibbon
    Jun 20 at 13:08
  • $\begingroup$ also don't forget the decompose and transpile step for the label gate(oracle), otherwise simulation will pop up error. @PGibbon $\endgroup$
    – poig
    Jun 20 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.