Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate Declaration #8402

Closed
Yelissal opened this issue Jul 26, 2022 · 2 comments · Fixed by #9953
Closed

Duplicate Declaration #8402

Yelissal opened this issue Jul 26, 2022 · 2 comments · Fixed by #9953
Labels
bug Something isn't working

Comments

@Yelissal
Copy link

Yelissal commented Jul 26, 2022

Environment

  • Qiskit Terra version:
  • Python version:
  • Operating system:

What is happening?

The qasm generated has multiple gates named the same thing. The parser can't handle because it doesn't know which one is valid.

How can we reproduce the issue?

import math
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

def CPhase(angle, exponent):
    qc = QuantumCircuit(1, name=f"U^{exponent}")
    qc.p(angle*exponent, 0)
    phase_gate = qc.to_gate().control(1)
    phase_gate.name = f"controled_phase_{angle}_{exponent}"

    return phase_gate, qc

qc = QuantumCircuit(4)
repetition = 1
for j in (range(3)):
        cu,Pgate= CPhase(2*math.pi*(1/8), repetition)
        qc.append(cu, [j, 3])  #this applied the controlled gate to each qubit 
        repetition *= 2
qc.qasm(filename = "qpe.qasm")

"Running this will give you the error"
QuantumCircuit.from_qasm_file("qpe.qasm")

What should happen?

The phase should be able to be applied through the for-loop without running into a duplicate error.

Any suggestions?

No response

@Yelissal Yelissal added the bug Something isn't working label Jul 26, 2022
@BramDo
Copy link

BramDo commented Aug 2, 2022

In the function CPhase(angle, exponent) a quantumcircuit is defined with only one qubit. If you add a line with for qc with 4 qubits it will give you the qasm file.

import math
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister

**qc = QuantumCircuit(4, name="U")**

def CPhase(angle, exponent):

    qc = QuantumCircuit(1, name=f"U^{exponent}")
    qc.p(angle*exponent, 0)
    phase_gate = qc.to_gate().control(1)
    phase_gate.name = f"controled_phase_{angle}_{exponent}"

    return phase_gate, qc

repetition = 1
for j in (range(3)):
        cu,Pgate= CPhase(2*math.pi*(1/8), repetition)
        qc.append(cu, [j, 3])  #this applied the controlled gate to each qubit 
        repetition *= 2
qc.qasm(filename = "qpe.qasm")

OPENQASM 2.0;
include "qelib1.inc";
gate mcphase(param0) q0,q1 { cp(pi) q0,q1; }
gate mcphase(param0) q0,q1 { cp(pi/2) q0,q1; }
gate mcphase(param0) q0,q1 { cp(pi/4) q0,q1; }
gate controled_phase_0_7853981633974483_1 q0,q1 { mcphase(pi/4) q0,q1; }
gate controled_phase_0_7853981633974483_2 q0,q1 { mcphase(pi/2) q0,q1; }
gate controled_phase_0_7853981633974483_4 q0,q1 { mcphase(pi) q0,q1; }
qreg q[5];
controled_phase_0_7853981633974483_1 q[0],q[3];
controled_phase_0_7853981633974483_2 q[1],q[3];
controled_phase_0_7853981633974483_4 `q[2],q[3];```

@jakelishman
Copy link
Member

Hi @Yelissal, thanks for the report. This happens unfortunately because our QASM 2 exporter really can't handle parametrised gates very well (see #7335), and ControlledGate (which appears when you use the .control() method) has a lot of problems converting itself to OpenQASM code. Right now, the only real work-around is to use qiskit.transpile with a basis_gates argument to re-synthesise the circuit in terms of more basic gates.

We're working on overhauling how all parameters and QASM conversions happen, but it's a bit of a slow process, because it's concurrent with a lot of new features. I can't promise when we'll have a direct fix. A more limited fix for your particular use-case might involve us reworking ControlledGate.qasm to correctly understand when gates need to be renamed (QuantumCircuit.qasm does this for most gates, but ControlledGate overrides the behaviour), but that will still be quite a tricky endeavour unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants