Sequential Quantum Gate Decomposer  v1.9.6
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 importlib
21 import pytest
22 import inspect
23 
25  """Tests for the inheritance structure of the CH gate in the Squander package"""
26 
27  @pytest.mark.parametrize("gate_class", [
28  "CH",
29  "CNOT",
30  "CRY",
31  "CZ",
32  "H",
33  "R",
34  "RX",
35  "RY",
36  "RZ",
37  "SX",
38  "SYC",
39  "U3",
40  "X",
41  "Y",
42  "Z",
43  'CSWAP'
44  ])
45  def test_gate_inheritance(self, gate_class):
46  r"""
47  Test that gates properly inherits from the base Gate class.
48  """
49 
50  from squander import Gate
51 
52  control_gates = ["CH","CNOT","CRY","CZ","SYC"]
53  multi_qubit_gates = ["CSWAP"]
54  # Test class inheritance
55  module = importlib.import_module('squander')
56  gate_cls = getattr(module, gate_class)
57 
58  assert issubclass(gate_cls, Gate)
59 
60  # Create an instance
61  if gate_class in control_gates:
62  gate_ins = gate_cls(3, 0, 1) # 3 qubits, target 0, control 1
63  elif gate_class in multi_qubit_gates:
64  gate_ins = gate_cls(3,[0,1],[2])
65  else:
66  gate_ins = gate_cls(3, 0)
67 
68  assert isinstance(gate_ins, Gate)
69 
70  @pytest.mark.parametrize("gate_class", [
71  "CH",
72  "CNOT",
73  "CRY",
74  "CZ",
75  "H",
76  "R",
77  "RX",
78  "RY",
79  "RZ",
80  "SX",
81  "SYC",
82  "U3",
83  "X",
84  "Y",
85  "Z",
86  'CSWAP'
87  ])
88  def test_gate_methods(self, gate_class):
89  r"""
90  Test that gate inherits methods from the base Gate class.
91  """
92 
93  from squander import Gate
94 
95  control_gates = ["CH","CNOT","CRY","CZ","SYC"]
96  multi_qubit_gates = ["CSWAP"]
97 
98  module = importlib.import_module('squander')
99  gate_cls = getattr(module, gate_class)
100 
101  # Create an instance of the base Gate class to get its methods
102  base_gate = Gate(3)
103  base_methods = [name for name, obj in inspect.getmembers(base_gate)
104  if (callable(obj) and not name.startswith('_'))]
105  # Create an instance
106  if gate_class in control_gates:
107  gate_ins = gate_cls(3, 0, 1) # 3 qubits, target 0, control 1
108  elif gate_class in multi_qubit_gates:
109  gate_ins = gate_cls(3,[1,0],[2])
110  else:
111  gate_ins = gate_cls(3, 0)
112  # Check that each method from the base class is available in the specific gate
113  for method_name in base_methods:
114  assert hasattr(gate_ins, method_name), f"{gate_class} is missing method {method_name}"
115 
116  # Test method results
117  assert gate_ins.get_Target_Qbit() == 0
118  if gate_class in control_gates:
119  assert gate_ins.get_Control_Qbit() == 1
120  elif gate_class in multi_qubit_gates:
121  assert gate_ins.get_Target_Qbits() == [0,1]
122  name = gate_ins.get_Name()
123  assert isinstance(name, str)
124  assert len(name) > 0
Base class for the representation of general gate operations.
Definition: Gate.h:86