Sequential Quantum Gate Decomposer  v1.9.3
Powerful decomposition of general unitarias into one- and two-qubit gates gates
test_R.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 numpy as np
21 import random
22 
23 from qiskit import QuantumCircuit
24 from qiskit.visualization import plot_histogram
25 
26 from squander.utils import get_unitary_from_qiskit_circuit
27 from squander.gates.gates_Wrapper import R
28 import math
29 from scipy.stats import unitary_group
30 
32  """This is a test class of the python iterface to the gates of the QGD package"""
33 
34 
35 
36  def test_R_get_matrix(self):
37  r"""
38  This method is called by pytest.
39  Test to create an instance of RX gate.
40  """
41 
42  pi=np.pi
43 
44  # parameters
45  parameters = np.array( [pi/2*0.32,np.pi/2*0.32] )
46 
47  for qbit_num in range(1,7):
48 
49  # target qbit
50  target_qbit = qbit_num-1
51 
52  # creating an instance of the C++ class
53  R_gate = R( qbit_num, target_qbit )
54 
55  #SQUANDER
56 
57  # get the matrix
58  R_squander = R_gate.get_Matrix( parameters )
59 
60  #QISKIT
61 
62  # Create a Quantum Circuit acting on the q register
63  circuit = QuantumCircuit(qbit_num)
64 
65  # Add the RX gate on qubit pi, pi,
66  circuit.r(parameters[0]*2,parameters[1], target_qbit)
67 
68  # the unitary matrix from the result object
69  R_qiskit = get_unitary_from_qiskit_circuit( circuit )
70  R_qiskit = np.asarray(R_qiskit)
71 
72  #the difference between the SQUANDER and the qiskit result
73  delta_matrix=R_squander-R_qiskit
74 
75  # compute norm of matrix
76  error=np.linalg.norm(delta_matrix)
77 
78  #print("Get_matrix: The difference between the SQUANDER and the qiskit result is: " , np.around(error,2))
79  assert( error < 1e-3 )
80 
81  def test_R_apply_to(self):
82  r"""
83  This method is called by pytest.
84  Test to create an instance of U3 gate and compare with qiskit.
85  """
86 
87  pi=np.pi
88 
89  # parameters
90  parameters = np.array( [pi/2*0.32,np.pi/2*0.32] )
91 
92  for qbit_num in range(1,7):
93 
94  # target qbit
95  target_qbit = qbit_num-1
96 
97  # creating an instance of the C++ class
98  R_gate = R( qbit_num, target_qbit )
99 
100  #create text matrix
101  test_matrix= np.identity( 2**qbit_num, dtype=np.complex128 )
102 
103  #QISKIT
104 
105  # Create a Quantum Circuit acting on the q register
106  circuit = QuantumCircuit(qbit_num)
107 
108  # Add the RX gate on qubit pi, pi,
109  circuit.r(parameters[0]*2,parameters[1], target_qbit)
110 
111  # the unitary matrix from the result object
112  R_qiskit = get_unitary_from_qiskit_circuit( circuit )
113  R_qiskit = np.asarray(R_qiskit)
114 
115  # Create a Quantum Circuit acting on the q register
116  circuit = QuantumCircuit(qbit_num)
117 
118  # apply the gate on the input array/matrix
119  #RX_qiskit_apply_gate=np.matmul(RX_qiskit, test_matrix)
120 
121  #SQUANDER
122 
123  R_squander=test_matrix
124  print(R_squander.dtype)
125 
126  # apply the gate on the input array/matrix
127  R_gate.apply_to( R_squander, parameters )
128 
129  #the difference between the SQUANDER and the qiskit result
130  delta_matrix=R_squander-R_qiskit
131 
132  # compute norm of matrix
133  error=np.linalg.norm(delta_matrix)
134 
135  #print("Apply_to: The difference between the SQUANDER and the qiskit result is: " , np.around(error,2))
136  assert( error < 1e-3 )
def get_unitary_from_qiskit_circuit
Call to retrieve the unitary from QISKIT circuit.
Definition: utils.py:52