3
$\begingroup$

I'm using the following code :

qw = QuantumCircuit(4)
coin = np.array([[1, -1, -1, -1],
                                [-1, 1, -1, -1],
                                [-1, -1, 1, -1],
                                [-1, -1, -1, 1]]) / 2

# Convert the unitary matrix to a UnitaryGate
coin = UnitaryGate(coin, label='C')

# ====== Initial State Preparation 

qw.h(0)
qw.h(1)
qw.h(2)
qw.h(3)

# ----------------------------------

qw.x(0)
qw.x(1)

grover = -np.array([[1, -1, -1, -1],
                                [-1, 1, -1, -1],
                                [-1, -1, 1, -1],
                                [-1, -1, -1, 1]]) / 2

grover = UnitaryGate(grover, label='G')

# Apply the controlled unitary operation with qubits 0 and 1 as controls
# and qubits 2 and 3 as targets
qw.append(grover.control(1), [0,2, 3])

qw.x(0)
qw.x(1)

# Visualize the circuit
qw.draw('mpl')

# Import Aer
from qiskit import Aer

# Run the quantum circuit on a statevector simulator backend
backend = Aer.get_backend('statevector_simulator')

# Create a Quantum Program for execution
job = backend.run(qw)
result = job.result()
outputstate = result.get_statevector(qw, decimals=3)
print(outputstate)`

I'm getting the following error : AerError: 'unknown instruction: c-unitary' It's coming from the use of controlled unitary. I'm not sure how to resolve this.

$\endgroup$
4
  • $\begingroup$ I’m voting to close this question because it's not about quantum computing. The appropriate venue for the question is any of the support channels provided by Aer (perhaps their github repo?). $\endgroup$ Oct 7, 2023 at 10:39
  • 2
    $\begingroup$ I disagree, this looks like it's about quantum conputing. $\endgroup$ Oct 7, 2023 at 12:39
  • $\begingroup$ Many scientific fields use computers. Consider a biologist having an exception raised by their python script due to a bug or misconfiguration. We don't consider "why does this code raise KeyError exception?" a question about biology. It's a question about software engineering. The reason we sometimes conflate the two on this site is because our tools and our subject matter of interest both concern certain types of computations. Nonetheless, software engineering and quantum computing are distinct if related fields. The former has excellent resources elsewhere which should be taken advantage of. $\endgroup$ Oct 8, 2023 at 11:38
  • 1
    $\begingroup$ @AdamZalcman: I see your point, however, we should distinguish between problems for example with installation of Python libraries and issues with algorithms construction. The latter definitely concerns quantum computing and I think that this is case of this question. In your example with biology, you would probably help an asker if an issue is cause by wrong manipulation with a library function because that is in context of biology rather than SW engineering. $\endgroup$ Oct 9, 2023 at 5:58

2 Answers 2

1
$\begingroup$

You need to call .decompose on a circuit to decompose it into simpler gates before running it on a simulator:

job = backend.run(qw.decompose(reps=6))

reps is the number of times the circuit will be decomposed (I figured the number by trial-and-error, increasing it until the various "unknown instruction" error messages were replaced by an output).

$\endgroup$
0
$\begingroup$

qiskit.transpile()

This function converts an input circuit into a set of gates that the simulator knows how to simulate. I don't know how it compares to decompose as mentioned at: https://quantumcomputing.stackexchange.com/a/34401/4243 but the fact that it does not require a hard to determine integer input is reassuring. Some documentation for it can be found at: https://docs.quantum.ibm.com/transpile. Example:

from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import QFT
from qiskit_aer import Aer, AerSimulator

qc = QuantumCircuit(3)
qft = QFT(num_qubits=3).to_gate()
qc.append(qft, qargs=range(3))
print(qc)

simulator = AerSimulator()
qct = transpile(qc, simulator)
print(qct)

state = Aer.get_backend('statevector_simulator').run(qct, shots=1).result().get_statevector()
print(state)

which outputs:

     ┌──────┐
q_0: ┤0     ├
     │      │
q_1: ┤1 QFT ├
     │      │
q_2: ┤2     ├
     └──────┘
                                          ┌───┐   
q_0: ────────────────────■────────■───────┤ H ├─X─
                   ┌───┐ │        │P(π/2) └───┘ │ 
q_1: ──────■───────┤ H ├─┼────────■─────────────┼─
     ┌───┐ │P(π/2) └───┘ │P(π/4)                │ 
q_2: ┤ H ├─■─────────────■──────────────────────X─
     └───┘                                        
Statevector([0.35355339+0.j, 0.35355339+0.j, 0.35355339+0.j,
             0.35355339+0.j, 0.35355339+0.j, 0.35355339+0.j,
             0.35355339+0.j, 0.35355339+0.j],
            dims=(2, 2, 2))

so we see that initially QFT was stored as a single gate on the circuit representation, and then transpile seems to break it up into primitives that the Aer simulator understands.

If in the above code we instead use run(qct then it blows up with:

qiskit_aer.aererror.AerError: 'unknown instruction: QFT'

UnitaryGate example

Here's a minimization of your specific problem:

from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import UnitaryGate
from qiskit_aer import Aer, AerSimulator

qc = QuantumCircuit(4)
qc.h(0)
qc.h(1)
qc.h(2)
qc.h(3)
qc.x(0)
qc.x(1)
grover = UnitaryGate([
  [ 1/2, -1/2, -1/2, -1/2],
  [-1/2,  1/2, -1/2, -1/2],
  [-1/2, -1/2,  1/2, -1/2],
  [-1/2, -1/2, -1/2,  1/2]
])
qc.append(grover.control(1), [0, 2, 3])
qc.x(0)
qc.x(1)
print(qc)
qc = transpile(qc, AerSimulator())
print(qc)
print(Aer.get_backend('statevector_simulator').run(qc).result().get_statevector())

Output:

     ┌───┐┌───┐            ┌───┐
q_0: ┤ H ├┤ X ├─────■──────┤ X ├
     ├───┤├───┤     │      ├───┤
q_1: ┤ H ├┤ X ├─────┼──────┤ X ├
     ├───┤└───┘┌────┴─────┐└───┘
q_2: ┤ H ├─────┤0         ├─────
     ├───┤     │  Unitary │     
q_3: ┤ H ├─────┤1         ├─────
     └───┘     └──────────┘     
global phase: 3.5869
         ┌─────────┐    ┌───┐┌───┐┌───┐┌──────────┐┌───┐┌──────────────┐┌───┐┌─────────┐┌───┐┌─────────┐┌───┐┌──────────────────────┐                ┌───┐┌─────────┐┌───┐┌─────────┐┌───┐»
q_0: ────┤ U2(0,0) ├────┤ X ├┤ X ├┤ X ├┤ Rz(3π/4) ├┤ X ├┤ U2(0,3.1067) ├┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├┤ U3(0.57213,-π/2,π/2) ├────────────────┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├»
         ├─────────┴┐   └─┬─┘└─┬─┘└─┬─┘└──────────┘└─┬─┘└──────────────┘└─┬─┘└─────────┘└─┬─┘└─────────┘└─┬─┘└──────────────────────┘                └─┬─┘└─────────┘└─┬─┘└─────────┘└─┬─┘»
q_1: ────┤ U2(0,-π) ├─────┼────┼────┼────────────────┼────────────────────┼───────────────┼───────────────┼────────────────────────────────────────────┼───────────────┼───────────────┼──»
     ┌───┴──────────┴──┐  │    │    │                │                    │               │               │        ┌─────────┐       ┌───┐┌─────────┐  │               │               │  »
q_2: ┤ U2(-0.78942,-π) ├──┼────■────┼────────────────■────────────────────■───────────────┼───────────────■────────┤ Unitary ├───────┤ X ├┤ Unitary ├──■───────────────┼───────────────■──»
     ├─────────────────┤  │         │                                                     │                        └─────────┘       └─┬─┘└─────────┘                  │  ┌─────────┐     »
q_3: ┤ U2(-0.96181,-π) ├──■─────────■─────────────────────────────────────────────────────■────────────────────────────────────────────■───────────────────────────────■──┤ Unitary ├─────»
     └─────────────────┘                                                                                                                                                  └─────────┘     »
«     ┌─────────┐                                     ┌─────────┐                ┌───┐┌─────────┐┌───┐┌─────────┐┌───┐┌─────────────────────┐┌───┐┌─────────┐┌───┐┌─────────┐┌───┐»
«q_0: ┤ Unitary ├──■───────────────────────────────■──┤ Unitary ├────────────────┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├┤ U3(1.3622,-π/2,π/2) ├┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├»
«     └─────────┘  │                               │  └─────────┘                └─┬─┘└─────────┘└─┬─┘└─────────┘└─┬─┘└─────────────────────┘└─┬─┘└─────────┘└─┬─┘└─────────┘└─┬─┘»
«q_1: ─────────────┼───────────────────────────────┼───────────────────────────────┼───────────────┼───────────────┼───────────────────────────┼───────────────┼───────────────┼──»
«                  │                  ┌─────────┐  │             ┌───┐┌─────────┐  │               │               │                           │               │               │  »
«q_2: ─────────────┼───────────────■──┤ Unitary ├──┼─────────────┤ X ├┤ Unitary ├──■───────────────┼───────────────■───────────────────────────■───────────────┼───────────────■──»
«                ┌─┴─┐┌─────────┐┌─┴─┐├─────────┤┌─┴─┐┌─────────┐└─┬─┘└─────────┘                  │                                                           │  ┌─────────┐     »
«q_3: ───────────┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├──■───────────────────────────────■───────────────────────────────────────────────────────────■──┤ Unitary ├─────»
«                └───┘└─────────┘└───┘└─────────┘└───┘└─────────┘                                                                                                 └─────────┘     »
«     ┌─────────┐                                                        ┌─────────┐┌───┐┌─────────┐┌───┐┌─────────┐┌───┐┌─────────────┐
«q_0: ┤ Unitary ├──■──────────────────────────────────────────────────■──┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├┤ Unitary ├┤ X ├┤ U2(-3π/4,0) ├
«     └─────────┘  │                                                  │  └─────────┘└─┬─┘└─────────┘└─┬─┘└─────────┘└─┬─┘└─────────────┘
«q_1: ─────────────┼──────────────────────────────────────────────────┼───────────────┼───────────────┼───────────────┼─────────────────
«     ┌─────────┐  │             ┌───┐┌────────────────────────────┐┌─┴─┐┌─────────┐  │               │               │                 
«q_2: ┤ Unitary ├──┼─────────────┤ X ├┤ U3(2.2827,-1.0409,-2.7761) ├┤ X ├┤ Unitary ├──■───────────────┼───────────────■─────────────────
«     └─────────┘┌─┴─┐┌─────────┐└─┬─┘└────────────────────────────┘└───┘└─────────┘                  │                                 
«q_3: ───────────┤ X ├┤ Unitary ├──■──────────────────────────────────────────────────────────────────■─────────────────────────────────
«                └───┘└─────────┘                                                                                                       
Statevector([-0.25+5.55111512e-17j,  0.25+1.01307851e-15j,
             -0.25+2.77555756e-17j,  0.25+1.06858966e-15j,
             -0.25-2.35922393e-16j,  0.25+4.44089210e-16j,
             -0.25-1.80411242e-16j,  0.25+3.88578059e-16j,
             -0.25+6.10622664e-16j,  0.25+9.71445147e-16j,
             -0.25+5.13478149e-16j,  0.25+8.46545056e-16j,
             -0.25+8.32667268e-17j,  0.25+4.44089210e-16j,
             -0.25+1.38777878e-16j,  0.25+4.16333634e-16j],
            dims=(2, 2, 2, 2))

So we see that it was also able to handle arbitrary unitaries, which it seemed to approximate with a bunch of smaller gates.

If I remove:

qc = transpile(qc, AerSimulator())

then it blows up with:

qiskit_aer.aererror.AerError: 'unknown instruction: c-unitary'

Tested on Python 3.11.6, Ubuntu 23.10,

qiskit==0.45.1
qiskit-aer==0.12.2
$\endgroup$

Your Answer

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

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