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

Fix global phase in inverse of UCGate #8508

Merged
merged 12 commits into from
Aug 14, 2022
3 changes: 3 additions & 0 deletions qiskit/extensions/quantum_initializer/uc.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def inverse(self):
definition = QuantumCircuit(*self.definition.qregs)
for inst in reversed(self._definition):
definition._append(inst.replace(operation=inst.operation.inverse()))

definition.global_phase = -self.definition.global_phase

inverse_gate.definition = definition
return inverse_gate

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
prelude: >
The ``inverse()`` of the ``UCGate`` class is defined by simply inverting the
existing decomposition, but the inverse of the global phase was missing.
fixes:
- |
Performs the inverse of the global phase in the ``inverse()`` function.
- |
Fixes the global phase problem of the isometry decomposition. Refer to
`#4687 <https://github.com/Qiskit/qiskit-terra/issues/4687>` for more
details.
israelferrazaraujo marked this conversation as resolved.
Show resolved Hide resolved
18 changes: 17 additions & 1 deletion test/python/circuit/test_uc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from qiskit.quantum_info.random import random_unitary
from qiskit.compiler import transpile
from qiskit.quantum_info.operators.predicates import matrix_equal
from qiskit.quantum_info import Operator

_id = np.eye(2, 2)
_not = np.matrix([[0, 1], [1, 0]])
Expand Down Expand Up @@ -71,7 +72,7 @@ def test_ucg(self, squs, up_to_diagonal):
self.assertTrue(matrix_equal(unitary_desired, unitary, ignore_phase=True))

def test_global_phase_ucg(self):
""" "Test global phase of uniformly controlled gates"""
"""Test global phase of uniformly controlled gates"""
gates = [random_unitary(2).data for _ in range(2**2)]
num_con = int(np.log2(len(gates)))
q = QuantumRegister(num_con + 1)
Expand All @@ -85,6 +86,21 @@ def test_global_phase_ucg(self):

self.assertTrue(np.allclose(unitary_desired, unitary))

def test_inverse_ucg(self):
israelferrazaraujo marked this conversation as resolved.
Show resolved Hide resolved
"""Test inverse function of uniformly controlled gates"""
gates = [random_unitary(2, seed=42 + s).data for s in range(2**2)]
num_con = int(np.log2(len(gates)))
q = QuantumRegister(num_con + 1)
qc = QuantumCircuit(q)

qc.uc(gates, q[1:], q[0], up_to_diagonal=False)
qc.append(qc.inverse(), qc.qubits)

unitary = Operator(qc).data
unitary_desired = np.identity(2**qc.num_qubits)

self.assertTrue(np.allclose(unitary_desired, unitary))


def _get_ucg_matrix(squs):
return block_diag(*squs)
Expand Down