Coverage for src/braket/circuits/gate.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
14from typing import Any, Sequence
16from braket.circuits.quantum_operator import QuantumOperator
17from braket.circuits.qubit_set import QubitSet
20class Gate(QuantumOperator):
21 """
22 Class `Gate` represents a quantum gate that operates on N qubits. Gates are considered the
23 building blocks of quantum circuits. This class is considered the gate definition containing
24 the metadata that defines what a gate is and what it does.
25 """
27 def __init__(self, qubit_count: int, ascii_symbols: Sequence[str]):
28 """
29 Args:
30 qubit_count (int): Number of qubits this gate interacts with.
31 ascii_symbols (Sequence[str]): ASCII string symbols for the gate. These are used when
32 printing a diagram of circuits. Length must be the same as `qubit_count`, and
33 index ordering is expected to correlate with target ordering on the instruction.
34 For instance, if CNOT instruction has the control qubit on the first index and
35 target qubit on the second index. Then ASCII symbols would have ["C", "X"] to
36 correlate a symbol with that index.
38 Raises:
39 ValueError: `qubit_count` is less than 1, `ascii_symbols` are None, or
40 `ascii_symbols` length != `qubit_count`
41 """
42 super().__init__(qubit_count=qubit_count, ascii_symbols=ascii_symbols)
44 def to_ir(self, target: QubitSet) -> Any:
45 """Returns IR object of quantum operator and target
47 Args:
48 target (QubitSet): target qubit(s)
49 Returns:
50 IR object of the quantum operator and target
51 """
52 raise NotImplementedError("to_ir has not been implemented yet.")
54 def __eq__(self, other):
55 if isinstance(other, Gate):
56 return self.name == other.name
57 return NotImplemented
59 def __repr__(self):
60 return f"{self.name}('qubit_count': {self.qubit_count})"
62 @classmethod
63 def register_gate(cls, gate: "Gate"):
64 """Register a gate implementation by adding it into the Gate class.
66 Args:
67 gate (Gate): Gate class to register.
68 """
69 setattr(cls, gate.__name__, gate)