Skip to content

Commit

Permalink
Define separate classes for CSIGN and CZ
Browse files Browse the repository at this point in the history
This makes sure sure that gate defined using CSIGN will always be recognized by isinstance(gate, CSIGN).
To compare the generated gate it is better to use the fidelity measure.
  • Loading branch information
BoxiLi committed May 22, 2022
1 parent ca52269 commit f585cce
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
36 changes: 31 additions & 5 deletions src/qutip_qip/operations/gateclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1026,9 +1026,39 @@ def get_compact_qobj(self):
return cnot()


class CZ(_OneControlledGate):
"""
Controlled Z gate. Identical to the CSIGN gate.
Examples
--------
>>> from qutip_qip.operations import CZ
>>> CSIGN(0, 1).get_compact_qobj() # doctest: +NORMALIZE_WHITESPACE
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = True
Qobj data =
[[ 1. 0. 0. 0.]
[ 0. 1. 0. 0.]
[ 0. 0. 1. 0.]
[ 0. 0. 0. -1.]]
"""

def __init__(self, controls, targets, **kwargs):
self.target_gate = Z
super().__init__(
targets=targets,
controls=controls,
target_gate=self.target_gate,
**kwargs,
)
self.name = "CZ"

def get_compact_qobj(self):
return csign()


class CSIGN(_OneControlledGate):
"""
Controlled Z gate.
Controlled CSIGN gate. Identical to the CZ gate.
Examples
--------
Expand Down Expand Up @@ -1056,10 +1086,6 @@ def get_compact_qobj(self):
return csign()


CZ = CSIGN
CZ.__doc__ = CZ.__doc__


class CPHASE(_OneControlledGate):
r"""
CPHASE gate.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def test_single_qubit_gates(self):

assert qc.gates[8].name == "T"
assert qc.gates[7].name == "S"
assert qc.gates[6].name == "CSIGN"
assert qc.gates[6].name == "CZ"
assert qc.gates[5].name == "CT"
assert qc.gates[4].name == "Z"
assert qc.gates[3].name == "CS"
Expand Down

0 comments on commit f585cce

Please sign in to comment.