Search code examples
quantum-computingqiskit

Keep receiving Error - AttributeError: 'QuantumCircuit' object has no attribute 'u3'


I am attempting to code a NOT gate on Qiskit and I keep receiving the error. I am not sure what to fix as I also want to run Quantum Battleships with partial NOT gates and it involves the same code.

q = QuantumRegister(1)
c = ClassicalRegister(1)
qc = QuantumCircuit(q,c)

qc.u3(math.pi,0,0, q[0])
qc.measure( q[0], c[0])

This was the main code and I get the following error:

AttributeError                            Traceback (most recent call last)
Cell In[4], line 5
      2 c = ClassicalRegister(1)
      3 qc = QuantumCircuit(q,c)
----> 5 qc.u3(math.pi,0,0, q[0])
      6 qc.measure( q[0], c[0])

AttributeError: 'QuantumCircuit' object has no attribute 'u3'

I tried to install programs that I thought I may have needed but I do not think I installed the right one.


Solution

  • u3 was deprecated and removed from Qiskit https://qiskit.org/documentation/release_notes.html#id379 - the original deprecation notice from Qiskit 0.16.0 with alternatives

    The QuantumCircuit methods u1, u2 and u3 are now deprecated. Instead the following replacements can be used.

    u1(theta) = p(theta) = u(0, 0, theta)
    u2(phi, lam) = u(pi/2, phi, lam) = p(pi/2 + phi) sx p(pi/2 lam)
    u3(theta, phi, lam) = u(theta, phi, lam) = p(phi + pi) sx p(theta + pi) sx p(lam)

    The gate classes themselves, U1Gate, U2Gate and U3Gate remain, to allow loading of old jobs.

    The removal was done in 0.23.0 and this upgrade was part of release note

    The QuantumCircuit methods u1, u2, u3, and their controlled variants cu1, cu3 and mcu1 have been removed following their deprecation in Qiskit Terra 0.16.0. This was to remove gate names that were usually IBM-specific, in favour of the more general methods p(), u(), cp() and cu(). The gate classes U1Gate, U2Gate and U3Gate are still available for use with QuantumCircuit.append(), so backends can still support bases with these gates explicitly given.