0
$\begingroup$

I am following the examples given in this qiskit article about the Grover's algorithm.

I am trying to use this method that is given:

def diffuser(nqubits):
    qc = QuantumCircuit(nqubits)
    # Apply transformation |s> -> |00..0> (H-gates)
    for qubit in range(nqubits):
        qc.h(qubit)
    # Apply transformation |00..0> -> |11..1> (X-gates)
    for qubit in range(nqubits):
        qc.x(qubit)
    # Do multi-controlled-Z gate
    qc.h(nqubits-1)
    qc.mct(list(range(nqubits-1)), nqubits-1)  # multi-controlled-toffoli
    qc.h(nqubits-1)
    # Apply transformation |11..1> -> |00..0>
    for qubit in range(nqubits):
        qc.x(qubit)
    # Apply transformation |00..0> -> |s>
    for qubit in range(nqubits):
        qc.h(qubit)
    # We will return the diffuser as a gate
    U_s = qc.to_gate()
    U_s.name = "U$_s$"
    return U_s

With this code:

backend = Aer.get_backend("statevector_simulator")

circuit = QuantumCircuit(psi) #psi is my quantumregister variable

# code to implement Hadamard transform and oracle

circuit.append(diffuser(3), psi) #psi is a 3 qubit quantum register
job = backend.run(c)

I think the issue is with the circuit.append() method; it only occurs when I uncomment that line.

The error I get is below:

Simulation failed and returned the following error message:
ERROR:  [Experiment 0] Circuit circuit-91 contains invalid instructions {"gates": {U$_s$}} for "statevector" method.

When I add this line of code to the end of the file:

v = job.result().get_statevector(c).measure()[0]

to get the output of the code, I end up with this error:

qiskit.exceptions.QiskitError: 'Data for experiment "circuit-91" could not be found.'

Does anybody know why this is happening?

$\endgroup$

1 Answer 1

2
$\begingroup$

You have to transpile your circuit before running it. So, before calling backend.run you have to add the following:

from qiskit import transpile

circuit = transpile(circuit, backend=backend)

This is because your circuit contains a composite gate (the one created by diffuser() method) which in turn contains another composite gate (mct) . Qiskit transpiler will unroll these gates to one- and two-qubit gates, then translate these gates into gates from the target backend's basis gate set.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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