Skip to content
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
95 commits
Select commit Hold shift + click to select a range
00c1030
add CircuitElement mixin class
ShellyGarion Sep 30, 2021
1bb861f
update Instruction class
ShellyGarion Sep 30, 2021
34317d9
reformat instruction.py
ShellyGarion Sep 30, 2021
ea0f117
Merge branch 'main' into circuit_element
levbishop Sep 30, 2021
4d35d3c
first pass on adding CircuitElement mixin to Clifford
alexanderivrii Oct 6, 2021
df319e3
Merge pull request #3 from alexanderivrii/circuit_element
ShellyGarion Oct 6, 2021
3dba19c
style edits
ShellyGarion Oct 6, 2021
a73cf37
add docstring in CircuitElement class
ShellyGarion Oct 6, 2021
946a44c
remove duplicate instruction
ShellyGarion Oct 6, 2021
c0ac0cd
remove unused import instruction from instructionset
ShellyGarion Oct 6, 2021
665efa9
adding @property to functions in instruction.py related to CircuitEle…
alexanderivrii Oct 7, 2021
44ac031
Merge pull request #5 from alexanderivrii/circuit_element
ShellyGarion Oct 7, 2021
16a2710
For Instruction class: fixes related to @property decorator, and addi…
alexanderivrii Oct 7, 2021
2c91561
Merge pull request #6 from alexanderivrii/circuit_element
ShellyGarion Oct 7, 2021
ce47b1a
fix comment following review
ShellyGarion Oct 7, 2021
fb613fc
remove setter from Instruction
ShellyGarion Oct 8, 2021
51c9a82
add name setter since CI is failing
ShellyGarion Oct 8, 2021
0fca3e6
update num_params
ShellyGarion Oct 8, 2021
83a1adc
making QuantumCircuit inherit QuantumElementMixin
alexanderivrii Oct 10, 2021
badde7c
making sure that to_instruction still gets called for Clifford and Qu…
alexanderivrii Oct 10, 2021
ef1c1eb
updating description for params
alexanderivrii Oct 10, 2021
a510115
Merge pull request #9 from alexanderivrii/circuit_element
ShellyGarion Oct 10, 2021
daf4a69
Merge branch 'main' into circuit_element
ShellyGarion Oct 11, 2021
44eecfe
refactor CircuitElement --> Operation
ShellyGarion Oct 13, 2021
37573b2
update comments
ShellyGarion Oct 13, 2021
3033663
style
ShellyGarion Oct 13, 2021
1a49f9b
revert to original Instruction class
ShellyGarion Oct 14, 2021
e1cbf4f
revert code in instructionset and quantumcircuit
ShellyGarion Oct 14, 2021
9dda899
revert instructionset import
ShellyGarion Oct 14, 2021
16e6b6c
make Gate an Operation Mixin
ShellyGarion Oct 14, 2021
f90d68c
update Gate
ShellyGarion Oct 14, 2021
a999d8d
remove params from gate.py
ShellyGarion Oct 18, 2021
38aa966
style
ShellyGarion Oct 18, 2021
cbb90e3
update quantumcircuit following review
ShellyGarion Oct 19, 2021
682c817
add to_instruction method to Operation
ShellyGarion Oct 19, 2021
63e69a5
style
ShellyGarion Oct 19, 2021
f408dfe
fix to_instruction method in Operation
ShellyGarion Oct 19, 2021
77be67c
fix to_instruction method in Operation
ShellyGarion Oct 19, 2021
674a899
remove to_instruction method from Operation
ShellyGarion Oct 19, 2021
2b714f1
style
ShellyGarion Oct 19, 2021
3c47c3d
adding Operation mixing to Barrier, Measure, and Reset
alexanderivrii Oct 20, 2021
ee40fb4
Merge pull request #11 from alexanderivrii/circuit_element2
ShellyGarion Oct 20, 2021
f53c1bf
implementing name, num_qubits, num_clbits directly in Operation; remo…
alexanderivrii Oct 26, 2021
dd6ecc8
copy-pasting is evil
alexanderivrii Oct 26, 2021
45b0a8b
Merge pull request #12 from alexanderivrii/circuit_element
ShellyGarion Oct 26, 2021
4b9188b
fix conflict with main branch
ShellyGarion Oct 26, 2021
4607f6f
fix clifford name to Clifford
ShellyGarion Oct 26, 2021
6f08181
add super to QuantumCircuit
ShellyGarion Oct 26, 2021
80f9b45
fix super in QuantumCircuit
ShellyGarion Oct 26, 2021
51ac2ce
Fixing name is QuantumCircuit constructor
alexanderivrii Oct 26, 2021
0af6037
Merge pull request #13 from alexanderivrii/circuit_element
ShellyGarion Oct 26, 2021
1ca8db2
replace params with parameters in super
ShellyGarion Oct 26, 2021
04093d7
revert super in QuantumCircuit
ShellyGarion Oct 27, 2021
464487d
Merge branch 'main' into main_shelly_5
ShellyGarion Oct 27, 2021
4afcc1b
add Operation mixin to CNOTDiheral class
ShellyGarion Oct 27, 2021
237db1a
Adding Operation mixin to isometry and initializer
alexanderivrii Oct 27, 2021
85ef076
Merge pull request #14 from alexanderivrii/circuit_element
ShellyGarion Oct 27, 2021
200e549
add Operation mixin to Pauli class
ShellyGarion Oct 27, 2021
aecf2f3
add release notes
ShellyGarion Oct 27, 2021
a17e7c2
change _name to name in Gate class
ShellyGarion Oct 31, 2021
f3b5d72
add description to Operation docstring
ShellyGarion Oct 31, 2021
aeea5f4
raising CircuitError to disallow direct instantiation of Operation (b…
alexanderivrii Nov 2, 2021
cb6d667
Eli pointed out that calling Operation.__init__ is bad Python practic…
alexanderivrii Nov 2, 2021
00f7b05
running black
alexanderivrii Nov 2, 2021
10bccc9
Merge pull request #17 from alexanderivrii/circuit_element
ShellyGarion Nov 2, 2021
ea961e3
temporarily remove _new_ in Operation class
ShellyGarion Nov 2, 2021
2b7e258
remove CircuitError import from Operation
ShellyGarion Nov 2, 2021
a964fd2
fix signature errors when estimating free parameters
ewinston Nov 3, 2021
ef87ef9
Merge pull request #18 from ewinston/shelly/circuit_element
ShellyGarion Nov 4, 2021
abe8187
add CircuitError back to Operation
ShellyGarion Nov 4, 2021
8db4cbe
Adding pylint-disable messages for C3XGate and MCXGate (both implemen…
alexanderivrii Nov 9, 2021
3b4cc6f
Disabling pylint complaints that __new__ does not use args and kwargs…
alexanderivrii Nov 9, 2021
4256d7b
Fixed comparison between str and constant string
alexanderivrii Nov 9, 2021
aa22f38
Adding test for Operation class
alexanderivrii Nov 9, 2021
6d48741
Reformatting using black
alexanderivrii Nov 9, 2021
5d8a6ea
Merge pull request #19 from alexanderivrii/circuit_element
ShellyGarion Nov 9, 2021
5f45ae1
remove Operation mixin from QuantumCircuit class
ShellyGarion Nov 10, 2021
0c07a44
remove QuantumCircuit from documentation and release notes
ShellyGarion Nov 10, 2021
4117677
update names to lower case
ShellyGarion Nov 11, 2021
2aee5c7
refactor: params --> operands
ShellyGarion Nov 11, 2021
1be374a
Merge branch 'main' into circuit_element
ShellyGarion Dec 8, 2021
e974e4c
Updating the Operation class following the meeting with the quantum s…
alexanderivrii Dec 9, 2021
7cb8a11
Merge pull request #26 from alexanderivrii/circuit_element
ShellyGarion Dec 9, 2021
316c6b6
style fixes
ShellyGarion Dec 9, 2021
bf638b6
fix lint
ShellyGarion Dec 9, 2021
d537fda
remove import Operation from test
ShellyGarion Dec 9, 2021
052b923
minor fixes
alexanderivrii Dec 13, 2021
a792b8f
Merge pull request #27 from alexanderivrii/circuit_element
ShellyGarion Dec 13, 2021
f898e1b
Merge branch 'main' into circuit_element
ShellyGarion Dec 13, 2021
3e56045
Merge branch 'main' into circuit_element
ShellyGarion Jan 9, 2022
cd7f373
revert line in Gate class
ShellyGarion Jan 9, 2022
2b14079
revert pylint disable in x.py
ShellyGarion Jan 9, 2022
54ec96e
revert changes in gate_utils in test
ShellyGarion Jan 10, 2022
72f54cf
add more detailed doc and an example
ShellyGarion Jan 11, 2022
d005e97
Merge branch 'main' into circuit_element
mergify[bot] Jan 11, 2022
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/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
from .controlledgate import ControlledGate
from .instruction import Instruction
from .instructionset import InstructionSet
from .circuit_element import CircuitElement
from .barrier import Barrier
from .delay import Delay
from .measure import Measure
Expand Down
49 changes: 49 additions & 0 deletions qiskit/circuit/circuit_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.

"""Circuit Element Mixin."""

from abc import ABC, abstractmethod


class CircuitElement(ABC):
"""Circuit Element Mixin Class."""

@property
@abstractmethod
def name(self):
"""Unique string identifier for operation type."""
raise NotImplementedError

@property
@abstractmethod
def num_qubits(self):
"""Number of qubits."""
raise NotImplementedError

@property
@abstractmethod
def num_clbits(self):
"""Number of classical bits."""
raise NotImplementedError

@property
@abstractmethod
def num_params(self):
"""Number of parameters."""
raise NotImplementedError

@property
@abstractmethod
def params(self):
"""List of parameters to specialize a specific CircuitElement instance."""
raise NotImplementedError
Comment thread
ShellyGarion marked this conversation as resolved.
Outdated
51 changes: 46 additions & 5 deletions qiskit/circuit/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@
from qiskit.qobj.qasm_qobj import QasmQobjInstruction
from qiskit.circuit.parameter import ParameterExpression
from .tools import pi_check
from .circuit_element import CircuitElement

_CUTOFF_PRECISION = 1e-10


class Instruction:
class Instruction(CircuitElement):
"""Generic quantum instruction."""

# Class attribute to treat like barrier for transpiler, unroller, drawer
Expand Down Expand Up @@ -75,9 +76,9 @@ def __init__(self, name, num_qubits, num_clbits, params, duration=None, unit="dt
raise CircuitError(
"bad instruction dimensions: %d qubits, %d clbits." % num_qubits, num_clbits
)
self.name = name
self.num_qubits = num_qubits
self.num_clbits = num_clbits
self._name = name
self._num_qubits = num_qubits
self._num_clbits = num_clbits

self._params = [] # a list of gate params stored
# Custom instruction label
Expand Down Expand Up @@ -430,7 +431,7 @@ def copy(self, name=None):
cpy = self.__deepcopy__()

if name:
cpy.name = name
cpy._name = name
return cpy

def __deepcopy__(self, _memo=None):
Expand Down Expand Up @@ -528,3 +529,43 @@ def repeat(self, n):
qc.data = [(self, qargs[:], cargs[:])] * n
instruction.definition = qc
return instruction

@property
def name(self):
"""Unique string identifier for operation type."""
return self._name

@name.setter
def name(self, name):
"""Set the name."""
self._name = name

Comment thread
ShellyGarion marked this conversation as resolved.
@property
def num_qubits(self):
"""Number of qubits."""
return self._num_qubits

@num_qubits.setter
def num_qubits(self, num_qubits):
"""Set num_qubits."""
self._num_qubits = num_qubits

Comment thread
ShellyGarion marked this conversation as resolved.
@property
def num_clbits(self):
"""Number of classical bits."""
return self._num_clbits

@num_clbits.setter
def num_clbits(self, num_clbits):
"""Set num_clbits."""
self._num_clbits = num_clbits

Comment thread
ShellyGarion marked this conversation as resolved.
Outdated
@property
def num_params(self):
"""Number of parameters."""
return self._num_params

@num_params.setter
def num_params(self, num_params):
"""Set num_params."""
self._num_params = num_params
Comment thread
ShellyGarion marked this conversation as resolved.
Outdated
6 changes: 3 additions & 3 deletions qiskit/circuit/instructionset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
Instruction collection.
"""
from qiskit.circuit.exceptions import CircuitError
from .instruction import Instruction
from .classicalregister import Clbit
from .circuit_element import CircuitElement


class InstructionSet:
Expand Down Expand Up @@ -46,8 +46,8 @@ def __getitem__(self, i):

def add(self, gate, qargs, cargs):
"""Add an instruction and its context (where it is attached)."""
if not isinstance(gate, Instruction):
raise CircuitError("attempt to add non-Instruction" + " to InstructionSet")
if not isinstance(gate, CircuitElement):
raise CircuitError("attempt to add non-CircuitElement" + " to InstructionSet")
self.instructions.append(gate)
self.qargs.append(qargs)
self.cargs.append(cargs)
Expand Down
15 changes: 9 additions & 6 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from .delay import Delay
from .measure import Measure
from .reset import Reset
from .circuit_element import CircuitElement

try:
import pygments
Expand Down Expand Up @@ -1124,7 +1125,7 @@ def cbit_argument_conversion(self, clbit_representation: ClbitSpecifier) -> List

def append(
self,
instruction: Instruction,
instruction: CircuitElement,
qargs: Optional[Sequence[QubitSpecifier]] = None,
cargs: Optional[Sequence[ClbitSpecifier]] = None,
) -> InstructionSet:
Expand All @@ -1144,7 +1145,9 @@ def append(
CircuitError: if object passed is neither subclass nor an instance of Instruction
"""
# Convert input to instruction
if not isinstance(instruction, Instruction) and not hasattr(instruction, "to_instruction"):
if not isinstance(instruction, CircuitElement) and not hasattr(
instruction, "to_instruction"
):
if issubclass(instruction, Instruction):
raise CircuitError(
"Object is a subclass of Instruction, please add () to "
Expand All @@ -1154,7 +1157,7 @@ def append(
raise CircuitError(
"Object to append must be an Instruction or have a to_instruction() method."
)
if not isinstance(instruction, Instruction) and hasattr(instruction, "to_instruction"):
if not isinstance(instruction, CircuitElement) and hasattr(instruction, "to_instruction"):
instruction = instruction.to_instruction()

# Make copy of parameterized gate instances
Expand All @@ -1172,7 +1175,7 @@ def append(
return instructions

def _append(
self, instruction: Instruction, qargs: Sequence[Qubit], cargs: Sequence[Clbit]
self, instruction: CircuitElement, qargs: Sequence[Qubit], cargs: Sequence[Clbit]
) -> Instruction:
"""Append an instruction to the end of the circuit, modifying
the circuit in place.
Expand All @@ -1189,8 +1192,8 @@ def _append(
CircuitError: if the gate is of a different shape than the wires
it is being attached to.
"""
if not isinstance(instruction, Instruction):
raise CircuitError("object is not an Instruction.")
if not isinstance(instruction, CircuitElement):
raise CircuitError("object is not a CircuitElement.")

# do some compatibility checks
self._check_dups(qargs)
Expand Down
22 changes: 20 additions & 2 deletions qiskit/quantum_info/operators/symplectic/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import numpy as np

from qiskit.exceptions import QiskitError
from qiskit.circuit import QuantumCircuit, Instruction
from qiskit.circuit import QuantumCircuit, Instruction, CircuitElement
from qiskit.circuit.library.standard_gates import IGate, XGate, YGate, ZGate, HGate, SGate
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.quantum_info.operators.operator import Operator
Expand All @@ -27,7 +27,7 @@
from .clifford_circuits import _append_circuit


class Clifford(BaseOperator, AdjointMixin):
class Clifford(BaseOperator, AdjointMixin, CircuitElement):
"""An N-qubit unitary operator from the Clifford group.

**Representation**
Expand Down Expand Up @@ -525,6 +525,24 @@ def _pad_with_identity(self, clifford, qargs):

return padded

# These implement the required methods of the CircuitElement mixin

@property
def name(self):
return "clifford"
Comment thread
ShellyGarion marked this conversation as resolved.
Outdated

@property
def num_params(self):
return 1

@property
def num_clbits(self):
return 0

@property
def params(self):
return (self._table,)


# Update docstrings for API docs
generate_apidocs(Clifford)