Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_gate_inheritance.py
Go to the documentation of this file.
1 '''
2 Copyright 2020 Peter Rakyta, Ph.D.
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/.
18 '''
19 
20 import pytest
21 import inspect
22 
24  """Tests for the inheritance structure of the CH gate in the Squander package"""
25 
26  @pytest.mark.parametrize("gate_class", [
27  "CH",
28  "CNOT",
29  "CRY",
30  "CZ",
31  "H",
32  "R",
33  "RX",
34  "RY",
35  "RZ",
36  "SX",
37  "SYC",
38  "U3",
39  "X",
40  "Y",
41  "Z"
42  ])
43  def test_gate_inheritance(self, gate_class):
44  r"""
45  Test that gates properly inherits from the base Gate class.
46  """
47 
48  from squander import Gate
49  exec(f"from squander import {gate_class}")
50 
51  # Test class inheritance
52  gate_cls = eval(gate_class)
53  assert issubclass(gate_cls, Gate)
54 
55  # Create an instance
56  if hasattr(gate_cls, "control_qbit"):
57  gate_ins = gate_cls(3, 0, 1) # 3 qubits, target 0, control 1
58  else:
59  gate_ins = gate_cls(3, 0)
60  assert isinstance(gate_ins, Gate)
61 
62  @pytest.mark.parametrize("gate_class", [
63  "CH",
64  "CNOT",
65  "CRY",
66  "CZ",
67  "H",
68  "R",
69  "RX",
70  "RY",
71  "RZ",
72  "SX",
73  "SYC",
74  "U3",
75  "X",
76  "Y",
77  "Z"
78  ])
79  def test_gate_methods(self, gate_class):
80  r"""
81  Test that gate inherits methods from the base Gate class.
82  """
83 
84  from squander import Gate
85  exec(f"from squander import {gate_class}")
86 
87  # Create an instance of the base Gate class to get its methods
88  base_gate = Gate(3)
89  base_methods = [name for name, obj in inspect.getmembers(base_gate)
90  if (callable(obj) and not name.startswith('_'))]
91 
92  gate_cls = eval(gate_class)
93 
94  # Create an instance
95  if hasattr(gate_cls, "control_qbit"):
96  gate_ins = gate_cls(3, 0, 1) # 3 qubits, target 0, control 1
97  else:
98  gate_ins = gate_cls(3, 0)
99 
100  # Check that each method from the base class is available in the specific gate
101  for method_name in base_methods:
102  assert hasattr(gate_ins, method_name), f"{gate_class} is missing method {method_name}"
103 
104  # Test method results
105  assert gate_ins.get_Target_Qbit() == 0
106  if hasattr(gate_cls, "control_qbit"):
107  assert gate_ins.get_Control_Qbit() == 1
108 
109  name = gate_ins.get_Name()
110  assert isinstance(name, str)
111  assert len(name) > 0
Base class for the representation of general gate operations.
Definition: Gate.h:73