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

Add Cifford.from_label method #4222

Merged
merged 7 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 54 additions & 1 deletion qiskit/quantum_info/operators/symplectic/clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
Clifford operator class.
"""
# pylint: disable=invalid-name, abstract-method

import re
import numpy as np

from qiskit.exceptions import QiskitError
from qiskit.circuit import QuantumCircuit, Instruction
from qiskit.extensions.standard import IGate, XGate, YGate, ZGate, HGate, SGate
from qiskit.quantum_info.operators.base_operator import BaseOperator
from qiskit.quantum_info.operators.operator import Operator
from qiskit.quantum_info.operators.scalar_op import ScalarOp
Expand Down Expand Up @@ -389,6 +390,58 @@ def from_circuit(circuit):
_append_circuit(clifford, circuit)
return clifford

@staticmethod
def from_label(label):
"""Return a tensor product of single-qubit Clifford gates.

Args:
label (string): single-qubit operator string.

Returns:
Clifford: The N-qubit Clifford operator.

Raises:
QiskitError: if the label contains invalid characters.

Additional Information:
The labels correspond to the single-qubit Cliffords are

* - Label
- Stabilizer
- Destabilizer
* - ``"I"``
- +Z
- +X
* - ``"X"``
- -Z
- +X
* - ``"Y"``
- -Z
- -X
* - ``"Z"``
- +Z
- -X
* - ``"H"``
- +X
- +Z
* - ``"S"``
- +Z
- +Y
"""
# Check label is valid
label_gates = {
'I': IGate(), 'X': XGate(), 'Y': YGate(),
'Z': ZGate(), 'H': HGate(), 'S': SGate()
}
if re.match(r'^[IXYZHS\-+]+$', label) is None:
raise QiskitError('Label contains invalid characters.')
# Initialize an identity matrix and apply each gate
num_qubits = len(label)
op = Clifford(np.eye(2 * num_qubits, dtype=np.bool))
for qubit, char in enumerate(reversed(label)):
_append_circuit(op, label_gates[char], qargs=[qubit])
return op

# ---------------------------------------------------------------------
# Internal helper functions
# ---------------------------------------------------------------------
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/clifford-from-label-4ac8a987109916ef.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Add :meth:`~qiskit.quantum_info.Clifford.from_label` method to the
:class:`qiskit.quantum_info.Clifford` class for initializing as the
tensor product of single-qubit I, X, Y, Z, H, or S gates.
12 changes: 12 additions & 0 deletions test/python/quantum_info/operators/symplectic/test_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,18 @@ def test_dict_round_trip(self, num_qubits):
value = Clifford.from_dict(target.to_dict())
self.assertEqual(value, target)

def test_from_label(self):
"""Test from_label method"""
label = 'IXYZHS'
CI = Clifford(IGate())
CX = Clifford(XGate())
CY = Clifford(YGate())
CZ = Clifford(ZGate())
CH = Clifford(HGate())
CS = Clifford(SGate())
target = CI.tensor(CX).tensor(CY).tensor(CZ).tensor(CH).tensor(CS)
self.assertEqual(Clifford.from_label(label), target)


if __name__ == '__main__':
unittest.main()