Skip to content

Commit

Permalink
Stop monkeypatching standard gates and move them to circuit/library/s…
Browse files Browse the repository at this point in the history
…tandard_gates (#4035)

* begin move to qiskit.circuit.gates

* move more gates

* move all standard gates

* prepare SQU for moving

not sure where to put it yet though

* Move description of gates to new location

Co-authored-by: Ali Javadi-Abhari <ali.javadi@ibm.com>

* move new gates to  circuit/gates, fix lint

* new gates: dcx, iswap, rzx

* fix test

somehow cannot have * before u=None in SQUGate, otherwise the test_extensions_standard.test_to_matrix test fails due to a wrong num of args

* return type is actually instructionset

* fix tests

* use circuit.gates in qiskit/ but keep old location in test

* update extensions import to import form circ/gates

* move equivalence lib to circuit/gates

* move to library/standard_gates + kevin's comments

* fix cyclic import and RST

* rename leftover locations

* add reno

* fix cases missed in merge

* Update qiskit/circuit/quantumcircuit.py

Co-Authored-By: Ali Javadi-Abhari <ajavadia@users.noreply.github.com>

* trailing whitespace

* fix barrier import

* allow gate import from qiskit.circuit

* fix rxx, ryy, rzz docs (#4275)

Co-authored-by: Ali Javadi-Abhari <ali.javadi@ibm.com>

* fix left comments: circuit/gates to circuit/library

* allow standard gate imports from circuit.library

this also includes the instructions barrier, measure and reset

* remove accidentally added file

* add #4218

Co-authored-by: Luciano Bello <luciano.bello@ibm.com>

* add #4294

Co-authored-by: Luciano Bello <luciano.bello@ibm.com>

* import from new location

* binding a submodule to name need py > 3.6

the circular dependency + name binding `import qiskit.circuit.library.standard_gates a gates` is only supported from python 3.7+

Co-authored-by: Ali Javadi-Abhari <ali.javadi@ibm.com>
Co-authored-by: Erick Winston <ewinston@us.ibm.com>
Co-authored-by: Luciano Bello <luciano.bello@ibm.com>
Co-authored-by: Ali Javadi-Abhari <ajavadia@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
6 people authored Apr 28, 2020
1 parent b78f87a commit 4bd91fe
Show file tree
Hide file tree
Showing 113 changed files with 6,061 additions and 5,622 deletions.
10 changes: 5 additions & 5 deletions examples/python/ibmq/using_qiskit_terra_level_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# Import the Qiskit modules
from qiskit import IBMQ, BasicAer
from qiskit.circuit import QuantumCircuit
from qiskit.extensions import SwapGate
from qiskit.circuit.library.standard_gates import SwapGate
from qiskit.compiler import assemble
from qiskit.providers.ibmq import least_busy
from qiskit.tools.monitor import job_monitor
Expand All @@ -44,15 +44,15 @@
qc1 = QuantumCircuit(4, 4)
qc1.h(0)
qc1.cx(0, 1)
qc1.measure([0,1], [0,1])
qc1.measure([0, 1], [0, 1])

# Making another circuit: GHZ State
qc2 = QuantumCircuit(4, 4)
qc2.h([0,1,2,3])
qc2.h([0, 1, 2, 3])
qc2.cx(0, 1)
qc2.cx(0, 2)
qc2.cx(0, 3)
qc2.measure([0,1,2,3], [0,1,2,3])
qc2.measure([0, 1, 2, 3], [0, 1, 2, 3])

# Setting up the backend
print("(Aer Backends)")
Expand Down Expand Up @@ -120,7 +120,7 @@
sim_job = qasm_simulator.run(qobj)

# Getting the result
sim_result=sim_job.result()
sim_result = sim_job.result()

# Show the results
print(sim_result.get_counts(qc1))
Expand Down
1 change: 1 addition & 0 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
from .controlledgate import ControlledGate
from .instruction import Instruction
from .instructionset import InstructionSet
from .barrier import Barrier
from .measure import Measure
from .reset import Reset
from .parameter import Parameter
Expand Down
12 changes: 6 additions & 6 deletions qiskit/circuit/add_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
# (C) Copyright IBM 2017, 2019.
#
# 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
Expand All @@ -11,9 +11,9 @@
# 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.
"""
Add control to operation if supported.
"""

"""Add control to operation if supported."""

from typing import Union, Optional

from qiskit.circuit.exceptions import CircuitError
Expand Down Expand Up @@ -51,7 +51,7 @@ def add_control(operation: Union[Gate, ControlledGate],
Controlled version of gate.
"""
import qiskit.extensions.standard as standard
import qiskit.circuit.library.standard_gates as standard
if ctrl_state is None:
ctrl_state = 2**num_ctrl_qubits - 1
if isinstance(operation, standard.RZGate) or operation.name == 'rz':
Expand Down Expand Up @@ -100,7 +100,7 @@ def control(operation: Union[Gate, ControlledGate],
# pylint: disable=cyclic-import
import qiskit.circuit.controlledgate as controlledgate
# pylint: disable=unused-import
import qiskit.extensions.standard.multi_control_rotation_gates
import qiskit.circuit.library.standard_gates.multi_control_rotation_gates

# check args
if num_ctrl_qubits == 0:
Expand Down
36 changes: 36 additions & 0 deletions qiskit/circuit/barrier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# 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.

"""Barrier instruction."""

from qiskit.exceptions import QiskitError
from .instruction import Instruction


class Barrier(Instruction):
"""Barrier instruction."""

def __init__(self, num_qubits):
"""Create new barrier instruction."""
super().__init__("barrier", num_qubits, 0, [])

def inverse(self):
"""Special case. Return self."""
return Barrier(self.num_qubits)

def broadcast_arguments(self, qargs, cargs):
yield [qarg for sublist in qargs for qarg in sublist], []

def c_if(self, classical, val):
raise QiskitError('Barriers are compiler directives and cannot be conditional.')
17 changes: 8 additions & 9 deletions qiskit/circuit/controlledgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# This code is part of Qiskit.
#
# (C) Copyright IBM 2019.
# (C) Copyright IBM 2017, 2019.
#
# 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
Expand All @@ -12,16 +12,15 @@
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
Controlled unitary gate.
"""
"""Controlled unitary gate."""

from typing import Tuple, List, Optional, Union
from qiskit.circuit.exceptions import CircuitError

from .gate import Gate
from . import QuantumRegister
from .quantumregister import QuantumRegister
from .quantumregister import Qubit
from .quantumcircuit import Clbit
from .classicalregister import Clbit

# pylint: disable=missing-return-doc

Expand Down Expand Up @@ -61,7 +60,7 @@ def __init__(self, name: str, num_qubits: int, params: List,
.. jupyter-execute::
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.extensions.standard import HGate
from qiskit.circuit.library.standard_gates import HGate
qr = QuantumRegister(3)
qc = QuantumCircuit(qr)
Expand All @@ -74,7 +73,7 @@ def __init__(self, name: str, num_qubits: int, params: List,
.. jupyter-execute::
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.extensions.standard import HGate
from qiskit.circuit.library.standard_gates import HGate
qc1 = QuantumCircuit(2)
qc1.x(0)
Expand Down Expand Up @@ -112,7 +111,7 @@ def definition(self) -> List:
if not self._definition:
self._define()
# pylint: disable=cyclic-import
from qiskit.extensions.standard import XGate, CXGate
from qiskit.circuit.library.standard_gates import XGate, CXGate
bit_ctrl_state = bin(self.ctrl_state)[2:].zfill(self.num_ctrl_qubits)
# hacky way to get register assuming single register
if self._definition:
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/equivalence_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# pylint: disable=invalid-name

from qiskit.extensions.standard.equivalence_library import StandardEquivalenceLibrary
from .library.standard_gates.equivalence_library import StandardEquivalenceLibrary
from .equivalence import EquivalenceLibrary

SessionEquivalenceLibrary = EquivalenceLibrary(base=StandardEquivalenceLibrary)
53 changes: 53 additions & 0 deletions qiskit/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,54 @@
.. currentmodule:: qiskit.circuit.library
Standard Gates
==============
.. autosummary::
:toctree: ../stubs/
Barrier
C3XGate
C4XGate
CCXGate
CHGate
CRXGate
CRYGate
CRZGate
CSwapGate
CU1Gate
CU3Gate
CXGate
CYGate
CZGate
HGate
IGate
Measure
MSGate
RCCXGate
RC3XGate
Reset
RXGate
RXXGate
RYGate
RYYGate
RZGate
RZZGate
RZXGate
SGate
SdgGate
SwapGate
iSwapGate
DCXGate
TdgGate
U1Gate
U2Gate
U3Gate
XGate
YGate
ZGate
Blueprint Circuit
=================
Expand Down Expand Up @@ -127,6 +175,11 @@
"""

from .standard_gates import *
from ..barrier import Barrier
from ..measure import Measure
from ..reset import Reset

from .blueprintcircuit import BlueprintCircuit
from .generalized_gates import Diagonal
from .generalized_gates import Permutation
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/boolean_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import numpy as np
from qiskit.circuit import QuantumRegister, QuantumCircuit
from qiskit.circuit.exceptions import CircuitError
from qiskit.extensions.standard import MCXGate
from qiskit.circuit.library.standard_gates import MCXGate


class AND(QuantumCircuit):
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/data_encoding/pauli_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from qiskit.circuit import QuantumCircuit
from qiskit.circuit import Parameter, ParameterVector
from qiskit.extensions.standard import HGate
from qiskit.circuit.library.standard_gates import HGate

from ..n_local.n_local import NLocal

Expand Down
3 changes: 2 additions & 1 deletion qiskit/circuit/library/n_local/n_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from itertools import combinations

import numpy
from qiskit import QuantumCircuit, transpile, QuantumRegister
from qiskit import QuantumCircuit, QuantumRegister
from qiskit.circuit import Instruction, Parameter, ParameterVector, ParameterExpression
from qiskit.circuit.parametertable import ParameterTable

Expand Down Expand Up @@ -914,6 +914,7 @@ def __str__(self) -> str:
Returns:
A single string representing this NLocal.
"""
from qiskit.compiler import transpile
basis_gates = ['id', 'x', 'y', 'z', 'h', 's', 't', 'sdg', 'tdg', 'rx', 'ry', 'rz',
'rxx', 'ryy', 'cx', 'cy', 'cz', 'ch', 'crx', 'cry', 'crz', 'swap',
'cswap', 'ccx', 'cu1', 'cu3', 'u1', 'u2', 'u3']
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/n_local/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import numpy as np

from qiskit.circuit import QuantumCircuit, Instruction
from qiskit.extensions.standard import RYGate, CZGate
from qiskit.circuit.library.standard_gates import RYGate, CZGate
from .two_local import TwoLocal


Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/n_local/ryrz.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from numpy import pi

from qiskit.circuit import QuantumCircuit, Instruction
from qiskit.extensions.standard import RYGate, RZGate, CZGate
from qiskit.circuit.library.standard_gates import RYGate, RZGate, CZGate
from .two_local import TwoLocal


Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/library/n_local/swaprz.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from numpy import pi

from qiskit.circuit import QuantumCircuit, Parameter
from qiskit.extensions.standard import RZGate
from qiskit.circuit.library.standard_gates import RZGate
from .two_local import TwoLocal


Expand Down
56 changes: 31 additions & 25 deletions qiskit/circuit/library/n_local/two_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

from qiskit import QuantumCircuit
from qiskit.circuit import Gate, Instruction, Parameter
import qiskit.extensions.standard as gates

from .n_local import NLocal
from ..standard_gates import (
IGate, XGate, YGate, ZGate, RXGate, RYGate, RZGate, HGate, SGate, SdgGate, TGate, TdgGate,
RXXGate, RYYGate, RZXGate, RZZGate, SwapGate, CXGate, CYGate, CZGate, CRXGate, CRYGate, CRZGate,
CHGate
)


class TwoLocal(NLocal):
Expand Down Expand Up @@ -208,30 +212,32 @@ def _convert_to_block(self, layer: Union[str, type, Gate, QuantumCircuit]) -> Qu
# as static types, which might be something they should have anyways
theta = Parameter('θ')
valid_layers = {
'ch': gates.CHGate(),
'cx': gates.CXGate(),
'cy': gates.CYGate(),
'cz': gates.CZGate(),
'crx': gates.CRXGate(theta),
'cry': gates.CRYGate(theta),
'crz': gates.CRZGate(theta),
'h': gates.HGate(),
'i': gates.IGate(),
'id': gates.IGate(),
'iden': gates.IGate(),
'rx': gates.RXGate(theta),
'rxx': gates.RXXGate(theta),
'ry': gates.RYGate(theta),
'ryy': gates.RYYGate(theta),
'rz': gates.RZGate(theta),
's': gates.SGate(),
'sdg': gates.SdgGate(),
'swap': gates.SwapGate(),
'x': gates.XGate(),
'y': gates.YGate(),
'z': gates.ZGate(),
't': gates.TGate(),
'tdg': gates.TdgGate(),
'ch': CHGate(),
'cx': CXGate(),
'cy': CYGate(),
'cz': CZGate(),
'crx': CRXGate(theta),
'cry': CRYGate(theta),
'crz': CRZGate(theta),
'h': HGate(),
'i': IGate(),
'id': IGate(),
'iden': IGate(),
'rx': RXGate(theta),
'rxx': RXXGate(theta),
'ry': RYGate(theta),
'ryy': RYYGate(theta),
'rz': RZGate(theta),
'rzx': RZXGate(theta),
'rzz': RZZGate(theta),
's': SGate(),
'sdg': SdgGate(),
'swap': SwapGate(),
'x': XGate(),
'y': YGate(),
'z': ZGate(),
't': TGate(),
'tdg': TdgGate(),
}

# try to exchange `layer` from a string to a gate instance
Expand Down
Loading

0 comments on commit 4bd91fe

Please sign in to comment.