TransWikia.com

Why do I get the error name operator is not defined?

Quantum Computing Asked on June 24, 2021

I checked this code multiple times, I am trying to reproduce the same code using Grover’s algorithm from qiskit summerschool:

 import numpy as np
 from qiskit import IBMQ, QuantumCircuit, Aer, execute
 from qiskit.quantum_info import Operator
 from qiskit.providers.ibmq import least_busy
 from qiskit.visualization import plot_histogram
 from qiskit.tools.jupyter import *
 provider = IBMQ.load_account()

def phase_oracle(n, indices_to_mark, name='Oracle'):
qc = QuantumCircuit(n, name=name)
oracle_matrix = np.identity(2**n)
for index_to_mark in indices_to_mark:
    oracle_matrix[index_to_mark, index_to_mark]= -1
qc.unitary(operator(oracle_matrix), range(n))
return qc

def diffuser(n):
qc=QuantumCircuit(n,name='Diff - "V"')
qc.h(range(n))
qc.append(phase_oracle(n,[0]),range(n))
qc.h(range(n))
return qc

def Grover(n, marked):
qc=QuantumCircuit(n,n)
r = int(np.round(np.pi/
(4*np.arcsin(np.sqrt(len(marked)/2**n)))-1/2))
print(f'{n} qubits, basis state {marked} marked, {r} 
rounds')
qc.h(range(n))
for _ in range(r):
    qc.append(phase_oracle(n,marked),range(n))
    qc.append(diffuser(n),range(n))
qc.measure(range(n),range(n))
return qc

n = 5
x = np.random.randint(2**n)
marked = [x]
qc = Grover(n, marked)

qc.draw()

I get the name operator error which I cannot figure out the reason:

 NameError                                 Traceback 
(most recent call last)
 <ipython-input-22-96635782dc30> in <module>
  2 x = np.random.randint(2**n)
  3 marked = [x]
  ----> 4 qc = Grover(n, marked)
  5 
  6 qc.draw()

 <ipython-input-20-f14e47e0af5d> in Grover(n, marked)
 20     qc.h(range(n))
 21     for _ in range(r):
  ---> 22         
 qc.append(phase_oracle(n,marked),range(n))
 23         qc.append(diffuser(n),range(n))
 24     qc.measure(range(n),range(n))

  <ipython-input-20-f14e47e0af5d> in phase_oracle(n, 
  indices_to_mark, name)
  4     for index_to_mark in indices_to_mark:
  5         oracle_matrix[index_to_mark, 
  index_to_mark]= -1
   ----> 6     qc.unitary(operator(oracle_matrix), 
   range(n))
  7     return qc
  8 

NameError: name ‘operator’ is not defined. Can anybody help me woth this?

One Answer

That is because on line 14 of your program, you wrote:

qc.unitary(operator(oracle_matrix), range(n))

when it should be:

qc.unitary(Operator(oracle_matrix), range(n))

You should have capitalized the O in operator. Changing that, I get the following output when executed your code:

5 qubits, basis state [15] marked, 4 rounds
     ┌───┐┌─────────┐┌─────────────┐┌─────────┐┌─────────────┐┌─────────┐»
q_0: ┤ H ├┤0        ├┤0            ├┤0        ├┤0            ├┤0        ├»
     ├───┤│         ││             ││         ││             ││         │»
q_1: ┤ H ├┤1        ├┤1            ├┤1        ├┤1            ├┤1        ├»
     ├───┤│         ││             ││         ││             ││         │»
q_2: ┤ H ├┤2 Oracle ├┤2 Diff - "V" ├┤2 Oracle ├┤2 Diff - "V" ├┤2 Oracle ├»
     ├───┤│         ││             ││         ││             ││         │»
q_3: ┤ H ├┤3        ├┤3            ├┤3        ├┤3            ├┤3        ├»
     ├───┤│         ││             ││         ││             ││         │»
q_4: ┤ H ├┤4        ├┤4            ├┤4        ├┤4            ├┤4        ├»
     └───┘└─────────┘└─────────────┘└─────────┘└─────────────┘└─────────┘»
c: 5/════════════════════════════════════════════════════════════════════»
                                                                         »
«     ┌─────────────┐┌─────────┐┌─────────────┐┌─┐            
«q_0: ┤0            ├┤0        ├┤0            ├┤M├────────────
«     │             ││         ││             │└╥┘┌─┐         
«q_1: ┤1            ├┤1        ├┤1            ├─╫─┤M├─────────
«     │             ││         ││             │ ║ └╥┘┌─┐      
«q_2: ┤2 Diff - "V" ├┤2 Oracle ├┤2 Diff - "V" ├─╫──╫─┤M├──────
«     │             ││         ││             │ ║  ║ └╥┘┌─┐   
«q_3: ┤3            ├┤3        ├┤3            ├─╫──╫──╫─┤M├───
«     │             ││         ││             │ ║  ║  ║ └╥┘┌─┐
«q_4: ┤4            ├┤4        ├┤4            ├─╫──╫──╫──╫─┤M├
«     └─────────────┘└─────────┘└─────────────┘ ║  ║  ║  ║ └╥┘
«c: 5/══════════════════════════════════════════╩══╩══╩══╩══╩═
«                                               0  1  2  3  4 

Correct answer by KAJ226 on June 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP