Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5k stars 2.32k forks source link

Support unary operand for ParamterExpression #3098

Closed chunfuchen closed 4 years ago

chunfuchen commented 4 years ago

Information

What is the current behavior?

Since ParameterExpression does not support unary operand, a parameterized circuit might be crashed during transpile, e.g. a circuit with parameterized cu3.

error for unsupporting unary operand.

~/Developer/Quantum/qiskit-terra/qiskit/extensions/standard/cu3.py in _define(self)
     46             (CnotGate(), [q[0], q[1]], []),
---> 47             (U3Gate(-self.params[0] / 2, 0, -(self.params[1] + self.params[2]) / 2), [q[1]], []),
     48             (CnotGate(), [q[0], q[1]], []),

TypeError: bad operand type for unary -: 'Parameter'

On the other hand, the current parameter expression only the real number operations, is it possible to support the operation with the complex number? I know at the end the gate parameter could be real but it might be complex during the computations.

Steps to reproduce the problem

from qiskit import transpile
from qiskit import QuantumRegister, QuantumCircuit
from qiskit.circuit import Parameter

qc = QuantumCircuit(2, name='a')
params = [Parameter('x0'), Parameter('x1'), Parameter('x2')]
qc.cu3(params[0], params[1], params[2], 0, 1)
t_qc = transpile(qc, basis_gates=['u1', 'u2', 'u3', 'cx'])

What is the expected behavior?

Transpiling without crash

Suggested solutions

Since the definition of cu3 gate uses unary operator, the ParameterExpression class needs to support it in order to be compiled to basis gates.

like this

    def __neg__(self):
        return self._apply_operation(operator.mul, -1.0)

On the other hand, for supporting complex numbers in ParameterExpression. I think we can postpone the checking whether or not the parameter is complex at the very end. like here: https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/circuit/parameterexpression.py#L73 I think that since terra has already use sympy to support the ParameterExpression, it should not too difficult to support complex number? (Maybe I am wrong, please feel free to correct me)

woodsp-ibm commented 4 years ago

This issue is preventing us merging a PR for parameterized ccts in Aqua to speed up performance. Any idea when a fix can be expected for this issue? Ideally we need it ASAP.

kdk commented 4 years ago

3148 adds support for unary negation for Parameters, which should resolve the issue in transpiling parameterized cu3 gates.

woodsp-ibm commented 4 years ago

Thanks, the Aqua PR that needed this now passes and has been merged.

1ucian0 commented 4 years ago

This issue seems fixed in the current master, probably via #3148