Skip to content

Commit

Permalink
revert computing matrix for trivial matrices (#4354)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewinston committed May 28, 2020
1 parent f29bf76 commit dcbabda
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
20 changes: 16 additions & 4 deletions qiskit/circuit/library/standard_gates/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import _compute_control_matrix
from qiskit.qasm import pi
from .t import TGate, TdgGate
from .s import SGate, SdgGate
Expand Down Expand Up @@ -153,6 +152,18 @@ class CHGate(ControlledGate):
\end{pmatrix}
"""
# Define class constants. This saves future allocation time.
_sqrt2o2 = 1 / numpy.sqrt(2)
_matrix1 = numpy.array([[1, 0, 0, 0],
[0, _sqrt2o2, 0, _sqrt2o2],
[0, 0, 1, 0],
[0, _sqrt2o2, 0, -_sqrt2o2]],
dtype=complex)
_matrix0 = numpy.array([[_sqrt2o2, 0, _sqrt2o2, 0],
[0, 1, 0, 0],
[_sqrt2o2, 0, -_sqrt2o2, 0],
[0, 0, 0, 1]],
dtype=complex)

def __init__(self, label=None, ctrl_state=None):
"""Create new CH gate."""
Expand Down Expand Up @@ -194,6 +205,7 @@ def inverse(self):

def to_matrix(self):
"""Return a numpy.array for the CH gate."""
return _compute_control_matrix(self.base_gate.to_matrix(),
self.num_ctrl_qubits,
ctrl_state=self.ctrl_state)
if self.ctrl_state:
return self._matrix1
else:
return self._matrix0
25 changes: 21 additions & 4 deletions qiskit/circuit/library/standard_gates/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import _compute_control_matrix


class SwapGate(Gate):
Expand Down Expand Up @@ -187,6 +186,23 @@ class CSwapGate(ControlledGate, metaclass=CSwapMeta):
|0, b, c\rangle \rightarrow |0, b, c\rangle
|1, b, c\rangle \rightarrow |1, c, b\rangle
"""
# Define class constants. This saves future allocation time.
_matrix1 = numpy.array([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]], dtype=complex)
_matrix0 = numpy.array([[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 1]], dtype=complex)

def __init__(self, label=None, ctrl_state=None):
"""Create new CSWAP gate."""
Expand Down Expand Up @@ -220,9 +236,10 @@ def inverse(self):

def to_matrix(self):
"""Return a numpy.array for the Fredkin (CSWAP) gate."""
return _compute_control_matrix(self.base_gate.to_matrix(),
self.num_ctrl_qubits,
ctrl_state=self.ctrl_state)
if self.ctrl_state:
return self._matrix1
else:
return self._matrix0


class FredkinGate(CSwapGate, metaclass=CSwapMeta):
Expand Down
17 changes: 13 additions & 4 deletions qiskit/circuit/library/standard_gates/y.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from qiskit.circuit.controlledgate import ControlledGate
from qiskit.circuit.gate import Gate
from qiskit.circuit.quantumregister import QuantumRegister
from qiskit.circuit._utils import _compute_control_matrix


class YGate(Gate):
Expand Down Expand Up @@ -172,6 +171,15 @@ class CYGate(ControlledGate, metaclass=CYMeta):
\end{pmatrix}
"""
# Define class constants. This saves future allocation time.
_matrix1 = numpy.array([[1, 0, 0, 0],
[0, 0, 0, -1j],
[0, 0, 1, 0],
[0, 1j, 0, 0]], dtype=complex)
_matrix0 = numpy.array([[0, 0, -1j, 0],
[0, 1, 0, 0],
[1j, 0, 0, 0],
[0, 0, 0, 1]], dtype=complex)

def __init__(self, label=None, ctrl_state=None):
"""Create new CY gate."""
Expand Down Expand Up @@ -202,9 +210,10 @@ def inverse(self):

def to_matrix(self):
"""Return a numpy.array for the CY gate."""
return _compute_control_matrix(self.base_gate.to_matrix(),
self.num_ctrl_qubits,
ctrl_state=self.ctrl_state)
if self.ctrl_state:
return self._matrix1
else:
return self._matrix0


class CyGate(CYGate, metaclass=CYMeta):
Expand Down

0 comments on commit dcbabda

Please sign in to comment.