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

Adding global_phase gate in qiskit-terra #9251

Merged
merged 93 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 92 commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
aa8f608
created gphase.py file
Oct 11, 2022
89157f8
Merge branch 'Qiskit:main' into branch1
sumit-kale Oct 11, 2022
e2c97a7
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into br…
Oct 11, 2022
14f5599
completed glbalphase file & test_gate_defin
Oct 25, 2022
edcf7ba
adding GphaseGate in @data in test_gate_defn
Oct 25, 2022
87a2ade
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into br…
Oct 25, 2022
1aac540
Merge branch 'branch1' of https://github.com/sumit-kale/qiskit-terra …
Oct 25, 2022
6a3e453
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into br…
Oct 31, 2022
8051f10
added reno add-global-phase
Nov 1, 2022
9be8cda
Merge branch 'main' of https://github.com/Qiskit/qiskit-terra into br…
Nov 1, 2022
d06e4f4
added inv and matrix test in test_exten
Nov 1, 2022
b5320ef
test_gphase_inv
Nov 3, 2022
55e874b
tox lint edit
Nov 3, 2022
c7b3390
renamed GLobalPhaseGate to Global_Phase_Gate
Nov 3, 2022
9f2ba8a
Changed the name to more competable GlobalPhaseGate class
Nov 3, 2022
9c9d77f
# pylint: disable=E1101
Nov 8, 2022
64f5d7f
# pylint: disable=no-member
Nov 8, 2022
22573b2
# pylint: error fixed
Nov 8, 2022
c3d484e
including phase in super
Nov 8, 2022
be943b0
Merge branch 'Qiskit:main' into branch1
sumit-kale Nov 8, 2022
86267bc
Merge branch 'branch1' of https://github.com/sumit-kale/qiskit-terra …
Nov 8, 2022
025912e
changed float to ParameterExp
Nov 8, 2022
618816c
modified test_globalphaseGate
Nov 21, 2022
32a4401
phase to params[0]
Nov 23, 2022
371fbc3
renamed global_phase.py to gphase
Nov 23, 2022
615c9fc
test gphase new in extentions_std
Nov 25, 2022
c910dcd
debug 1
Nov 26, 2022
0661cd5
debug 2
Nov 27, 2022
b071cd6
debug 3 instructions set
Nov 28, 2022
6461819
debug 4
Nov 28, 2022
ab74fd9
debug 5
Nov 28, 2022
afd9a24
debug 6
Nov 29, 2022
0edba4c
debug 6 instructions set
Nov 29, 2022
fa4baa3
debug 7
Nov 29, 2022
4373619
debug 7
Nov 29, 2022
1f6b742
added gphase in quantumcircuit.py
Nov 30, 2022
ba96b3f
added method gphase in quantumcircuit.py
Nov 30, 2022
3df2949
test_gphase_inv
Dec 2, 2022
89be2d0
gphase debug1
Dec 2, 2022
1729cfc
gphase debug2
Dec 2, 2022
dfa77aa
gphase debug3
Dec 2, 2022
19ea5b1
inverse mtd in gphase.py
Dec 2, 2022
8a2b11f
added _array in gphase.py
Dec 3, 2022
4b40a04
gphase.py array debug1
Dec 3, 2022
201e30c
test_matrix gphase complex dtype
Dec 3, 2022
36e38d2
test_matrix in @ddt
Dec 6, 2022
1fb12c5
test_gphase_matrix
Dec 6, 2022
d643e03
changed gphase to global_phase
Dec 6, 2022
e1fa6dc
removed extra comma l871
Dec 6, 2022
efa551a
typo in reno fixed
Dec 6, 2022
b42dd84
modified reno
Dec 6, 2022
9aa08c4
Delete .vs directory
sumit-kale Dec 7, 2022
bb94760
Added test to check compatibility of gate with qc.attri
Dec 7, 2022
b8beb27
debugged pylint error1
Dec 7, 2022
7be3938
debugged pylint error2
Dec 7, 2022
0f384c0
debugged pylint error2a
Dec 7, 2022
c2fb3cd
debugged -epy error debug1
Dec 7, 2022
f441c0a
debugged -epy error debug 1
Dec 7, 2022
422a991
debugged -epy error debug 2
Dec 7, 2022
1d405bc
Merge branch 'global_phase' of https://github.com/sumit-kale/qiskit-t…
Dec 7, 2022
642877b
restored test_gate_definations
sumit-kale Dec 8, 2022
e0bec24
Revert "restored test_gate_definations"
Dec 8, 2022
ae83f16
reverted mistakely edited the file on Github
Dec 8, 2022
08c7ef5
Merge branch 'Qiskit:main' into global_phase
sumit-kale Dec 8, 2022
0c139a6
Delete .vs directory
sumit-kale Dec 7, 2022
0672ab3
restored test_gate_definations
sumit-kale Dec 8, 2022
72259b7
Revert "restored test_gate_definations"
Dec 8, 2022
a1afc5c
reverted mistakely edited the file on Github
Dec 8, 2022
2599360
Merge branch 'main' into global_phase
sumit-kale Dec 8, 2022
a2b6256
retrived test_gate_definitions
Dec 9, 2022
be489a0
Merge branch 'global_phase' of https://github.com/sumit-kale/qiskit-t…
Dec 9, 2022
ba0db3e
PR modifications new
Feb 3, 2023
87f1a0c
commit feb 13
Feb 14, 2023
48f7c12
range in append
Feb 16, 2023
d79782b
[] in append
Feb 16, 2023
bd46049
[0] in append
Feb 16, 2023
8c05971
[0]->[]
Feb 23, 2023
91ac5fc
[0]
Mar 10, 2023
26e0802
[]
Mar 20, 2023
adaa23a
Operator(qc)
Mar 23, 2023
8e431eb
fixed test_ex_std
Mar 30, 2023
2a15a6b
resolved changes suggested by Cryolis
Mar 30, 2023
166214f
Update global_phase.py
sumit-kale Apr 2, 2023
57939ba
Merge branch 'main' into global_phase
sumit-kale Apr 10, 2023
e4989e9
..
Apr 10, 2023
098d85b
resolved merge conflicts
Apr 10, 2023
a1bac05
Added transpiler consistancy test
Apr 11, 2023
eedbf55
restored the docstring
sumit-kale Apr 16, 2023
546872e
prevented cyclic imports and other changes
Apr 16, 2023
394e4f4
Fix spacing in docs
jakelishman Apr 17, 2023
7dc02bb
Merge remote-tracking branch 'ibm/main' into global_phase
jakelishman Apr 17, 2023
eb2b7bc
Fix lint
jakelishman Apr 17, 2023
64ea9e6
Add test of controlling global phase
jakelishman Apr 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions qiskit/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
XGate
YGate
ZGate
GlobalPhaseGate

Standard Directives
===================
Expand Down
3 changes: 2 additions & 1 deletion qiskit/circuit/library/standard_gates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from .x import MCXGate, MCXGrayCode, MCXRecursive, MCXVChain
from .y import YGate, CYGate
from .z import ZGate, CZGate, CCZGate

from .global_phase import GlobalPhaseGate
from .multi_control_rotation_gates import mcrx, mcry, mcrz


Expand Down Expand Up @@ -80,6 +80,7 @@ def get_standard_gate_name_mapping():
CYGate(),
CZGate(),
CCZGate(),
GlobalPhaseGate(Parameter("ϴ")),
HGate(),
PhaseGate(Parameter("ϴ")),
RCCXGate(),
Expand Down
62 changes: 62 additions & 0 deletions qiskit/circuit/library/standard_gates/global_phase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Global Phase Gate"""

from typing import Optional
import numpy
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit.quantumcircuit import QuantumCircuit
from qiskit.circuit.parameterexpression import ParameterValueType


class GlobalPhaseGate(Gate):
r"""The global phase gate (:math:`e^{i\theta}`).

Can be applied to a :class:`~qiskit.circuit.QuantumCircuit`

**Mathamatical Representation:**

.. math::
\text{GlobalPhaseGate}\ =
\begin{pmatrix}
e^{i\theta}
\end{pmatrix}
"""

def __init__(self, phase: ParameterValueType, label: Optional[str] = None):
"""
Args:
phase: The value of phase it takes.
label: An optional label for the gate.
"""
super().__init__("global_phase", 0, [phase], label=label)

def _define(self):

q = QuantumRegister(0, "q")
qc = QuantumCircuit(q, name=self.name, global_phase=self.params[0])

self.definition = qc

def inverse(self):
r"""Return inverted GLobalPhaseGate gate.

:math:`\text{GlobalPhaseGate}(\lambda){\dagger} = \text{GlobalPhaseGate}(-\lambda)`
"""
return GlobalPhaseGate(-self.params[0])

def __array__(self, dtype=complex):
"""Return a numpy.array for the global_phase gate."""
theta = self.params[0]
return numpy.array([[numpy.exp(1j * theta)]], dtype=dtype)
2 changes: 1 addition & 1 deletion qiskit/quantum_info/operators/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def compose(self, other, qargs=None, front=False):
tensor = np.reshape(self.data, self._op_shape.tensor_shape)
mat = np.reshape(other.data, other._op_shape.tensor_shape)
indices = [num_indices - 1 - qubit for qubit in qargs]
final_shape = [np.product(output_dims), np.product(input_dims)]
final_shape = [int(np.product(output_dims)), int(np.product(input_dims))]
data = np.reshape(
Operator._einsum_matmul(tensor, mat, indices, shift, right_mul), final_shape
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Added :class:`.GlobalPhaseGate` which can be applied to add a global phase
on the :class:`.QuantumCircuit`.

49 changes: 47 additions & 2 deletions test/python/circuit/test_extensions_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import numpy as np
from scipy.linalg import expm
from ddt import data, ddt, unpack

from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, execute
from qiskit.exceptions import QiskitError
from qiskit.circuit.exceptions import CircuitError
Expand All @@ -36,11 +35,14 @@
RZGate,
XGate,
YGate,
GlobalPhaseGate,
)
from qiskit import BasicAer
from qiskit.quantum_info import Pauli
from qiskit.quantum_info.operators.predicates import matrix_equal, is_unitary_matrix
from qiskit.utils.optionals import HAS_TWEEDLEDUM
from qiskit.quantum_info import Operator
from qiskit import transpile


class TestStandard1Q(QiskitTestCase):
Expand Down Expand Up @@ -851,6 +853,50 @@ def test_z_reg_inv(self):
self.assertEqual(instruction_set[1].qubits, (self.qr[1],))
self.assertEqual(instruction_set[2].operation.params, [])

def test_global_phase(self):
qc = self.circuit
qc.append(GlobalPhaseGate(0.1), [])
self.assertEqual(self.circuit[0].operation.name, "global_phase")
self.assertEqual(self.circuit[0].operation.params, [0.1])
self.assertEqual(self.circuit[0].qubits, ())

def test_global_phase_inv(self):
instruction_set = self.circuit.append(GlobalPhaseGate(0.1), []).inverse()
self.assertEqual(len(instruction_set), 1)
self.assertEqual(instruction_set[0].operation.params, [-0.1])

def test_global_phase_matrix(self):
"""Test global_phase matrix."""
theta = 0.1
np.testing.assert_allclose(
np.array(GlobalPhaseGate(theta)),
np.array([[np.exp(1j * theta)]], dtype=complex),
atol=1e-7,
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test to check consistency with the QuantumCircuit.global_phase attribute. Do this by creating two circuits, one that has a GlobalPhaseGate, and the other that is initialized by passing the global phase as argument. Check that the two circuits have the same unitary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new test has been added in to check the compatibility of the GLobalPhaseGate with the preexisting feature.


def test_global_phase_consistency(self):
"""Tests compatibility of GlobalPhaseGate with QuantumCircuit.global_phase"""
theta = 0.1
qc1 = QuantumCircuit(0, global_phase=theta)
qc2 = QuantumCircuit(0)
qc2.append(GlobalPhaseGate(theta), [])
np.testing.assert_allclose(
Operator(qc1),
Operator(qc2),
atol=1e-7,
)

def test_transpile_global_phase_consistency(self):
"""Tests compatibility of transpiled GlobalPhaseGate with QuantumCircuit.global_phase"""
qc1 = QuantumCircuit(0, global_phase=0.3)
qc2 = QuantumCircuit(0, global_phase=0.2)
qc2.append(GlobalPhaseGate(0.1), [])
np.testing.assert_allclose(
Operator(transpile(qc1, basis_gates=["u"])),
Operator(transpile(qc2, basis_gates=["u"])),
atol=1e-7,
)
sumit-kale marked this conversation as resolved.
Show resolved Hide resolved


@ddt
class TestStandard2Q(QiskitTestCase):
Expand Down Expand Up @@ -1375,7 +1421,6 @@ def test_to_matrix(self):
def test_to_matrix_op(self):
"""test gates implementing to_matrix generate matrix which matches
definition using Operator."""
from qiskit.quantum_info import Operator
from qiskit.circuit.library.generalized_gates.gms import MSGate
from qiskit.circuit.library.generalized_gates.pauli import PauliGate
from qiskit.circuit.library.pauli_evolution import PauliEvolutionGate
Expand Down