4 Created on Tue Jun 30 15:44:26 2020 5 Copyright 2020 Peter Rakyta, Ph.D. 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 11 http://www.apache.org/licenses/LICENSE-2.0 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 19 You should have received a copy of the GNU General Public License 20 along with this program. If not, see http://www.gnu.org/licenses/. 22 @author: Peter Rakyta, Ph.D. 33 from qiskit
import QuantumCircuit
36 qiskit_version = qiskit.version.get_version_info()
38 if qiskit_version[0] ==
'0':
39 from qiskit
import Aer
40 from qiskit
import execute
41 if int(qiskit_version[2])>3:
42 from qiskit.quantum_info
import Operator
44 import qiskit_aer
as Aer
45 from qiskit
import transpile
46 from qiskit.quantum_info
import Operator
54 Call to extract a unitary from Qiskit circuit 58 circuit (QuantumCircuit) A Qiskit circuit 62 Returns with the generated unitary 67 if qiskit_version[0] ==
'0':
68 backend = Aer.get_backend(
'aer_simulator')
69 circuit.save_unitary()
72 job = execute(circuit, backend)
79 circuit.save_unitary()
80 backend = Aer.AerSimulator(method=
'unitary')
82 compiled_circuit = transpile(circuit, backend)
83 result = backend.run(compiled_circuit).
result()
87 return np.asarray( result.get_unitary(circuit) )
95 Call to extract a unitary from Qiskit circuit using qiskit.quantum_info.Operator support 99 circuit (QuantumCircuit) A Qiskit circuit 103 Returns with the generated unitary 108 if qiskit_version[0] ==
'0' and int(qiskit_version[2])<4:
110 print(
"Currently installed version of qiskit does not support extracting the unitary of a circuit via Operator. Using get_unitary_from_qiskit_circuit function instead.")
113 return Operator(circuit).to_matrix()
119 Converts a QASM file to a SQUANDER circuit 123 filename (str) The path to the QASM file 127 Returns with the SQUANDER circuit and the array of the corresponding parameters 131 qc = qiskit.QuantumCircuit.from_qasm_file(filename)
133 allowed_gates = {
'u', 'u3', 'cx', 'cry', 'cz', 'ch', 'rx', 'ry', 'rz', 'h', 'x', 'y', 'z', 'sx'}
135 if any(gate.operation.name
not in allowed_gates
for gate
in qc.data):
136 qc_transpiled = qiskit.transpile(qc, basis_gates=allowed_gates, optimization_level=0)
140 circuit_squander, circut_parameters = Qiskit_IO.convert_Qiskit_to_Squander(qc_transpiled)
142 if return_transpiled:
return circuit_squander, circut_parameters, qc_transpiled
143 return circuit_squander, circut_parameters
148 def CompareCircuits( circ1: Circuit, parameters1: np.ndarray, circ2: Circuit, parameters2: np.ndarray, parallel : int = 1, tolerance: float = 1e-5) :
150 Call to test if the two circuits give the same state transformation upon a random input state 155 circ1 ( Circuit ) A circuit 157 parameters1 ( np.ndarray ) A parameter array associated with the input circuit 159 circ2 ( Circuit ) A circuit 161 parameters2 ( np.ndarray ) A parameter array associated with the input circuit 163 parallel (int, optional) Set 0 for sequential evaluation, 1 for using TBB parallelism or 2 for using openMP 165 tolerance ( float, optional) The tolerance of the comparision when the inner product of the resulting states is matched to unity. 170 Returns with True if the two circuits give identical results. 174 qbit_num1 = circ1.get_Qbit_Num()
175 qbit_num2 = circ2.get_Qbit_Num()
177 if qbit_num1 != qbit_num2:
178 raise Exception(
"The two compared circuits should have the same number of qubits." )
180 matrix_size = 1 << qbit_num1
181 initial_state_real = np.random.uniform(-1.0,1.0, (matrix_size,) )
182 initial_state_imag = np.random.uniform(-1.0,1.0, (matrix_size,) )
183 initial_state = initial_state_real + initial_state_imag*1j
184 norm = np.sum(initial_state_real * initial_state_real + initial_state_imag*initial_state_imag)
185 initial_state = initial_state/np.sqrt(norm)
189 transformed_state_1 = initial_state.copy()
190 transformed_state_2 = initial_state
192 circ1.apply_to( parameters1, transformed_state_1, parallel=parallel )
193 circ2.apply_to( parameters2, transformed_state_2, parallel=parallel)
195 overlap = np.sum( transformed_state_1.conj() * transformed_state_2 )
198 assert( (np.abs(overlap)-1) < tolerance )
def qasm_to_squander_circuit
def get_unitary_from_qiskit_circuit_operator
def get_unitary_from_qiskit_circuit
Call to retrieve the unitary from QISKIT circuit.
result
the result of the Qiskit job