forked from qiskit-community/qiskit-aqua
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] add test. Note qiskit cu3 has issues: Qiskit/qiskit#546
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright 2018 IBM. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ============================================================================= | ||
|
||
import numpy as np | ||
from scipy import linalg as scila | ||
import unittest | ||
from qiskit import QuantumCircuit, QuantumRegister | ||
from qiskit.wrapper import execute as q_execute | ||
from qiskit.tools.qi.qi import state_fidelity | ||
from test.common import QiskitAquaTestCase | ||
from qiskit_aqua.utils.controlledcircuit import get_controlled_circuit as gcc | ||
from qiskit_aqua.operator import Operator | ||
from qiskit_aqua import get_initial_state_instance | ||
|
||
|
||
def U(theta, phi, lam): | ||
return np.array( | ||
[ | ||
[np.cos(theta/2), -np.exp(1j*lam) * np.sin(theta/2)], | ||
[np.exp(1j*phi) * np.sin(theta/2), np.exp(1j*(phi+lam)) * np.cos(theta/2)] | ||
] | ||
) | ||
|
||
|
||
def U1(lam): | ||
return U(0, 0, lam) | ||
|
||
|
||
class TestControlledCircuit(QiskitAquaTestCase): | ||
def test_controlled_circuit(self): | ||
np.random.seed(0) | ||
num_base_qubits = 1 | ||
|
||
theta, phi, lam = 0, 0, 1 | ||
matrix_base_exponentiated = U1(lam) | ||
self.log.debug('base exp:\n{}'.format(matrix_base_exponentiated)) | ||
|
||
matrix_controlled_exponentiated = np.kron( | ||
[[1, 0], [0, 0]], | ||
np.eye(2 ** num_base_qubits) | ||
) + np.kron( | ||
[[0, 0], [0, 1]], | ||
matrix_base_exponentiated | ||
) | ||
self.log.debug('controlled exp:\n{}'.format(matrix_controlled_exponentiated)) | ||
|
||
state_in = get_initial_state_instance('CUSTOM') | ||
state_in.init_args(num_base_qubits + 1, state='uniform') | ||
state_in_vec = state_in.construct_circuit('vector') | ||
self.log.debug('state in: {}'.format(state_in_vec)) | ||
|
||
# get the exact state_out from raw matrix multiplication | ||
state_out_exact = matrix_controlled_exponentiated @ state_in_vec | ||
|
||
self.log.debug('exact: {}'.format(state_out_exact)) | ||
|
||
|
||
|
||
|
||
qr_base = QuantumRegister(num_base_qubits, name='q') | ||
qr_control = QuantumRegister(1, name='c') | ||
|
||
circuit_base = QuantumCircuit(qr_base) | ||
# circuit_base.u3(0, 0, lam, qr_base) | ||
circuit_base.u1(lam, qr_base) | ||
|
||
circuit_controlled = QuantumCircuit(qr_control, qr_base) | ||
circuit_controlled.h(qr_base) | ||
circuit_controlled.h(qr_control) | ||
gcc(circuit_base, qr_control[0], tgt_circuit=circuit_controlled, use_basis_gates=True) | ||
self.log.debug(circuit_controlled.qasm()) | ||
|
||
job = q_execute(circuit_controlled, 'local_statevector_simulator', skip_transpiler=False) | ||
state_out_circuit = np.asarray(job.result().get_statevector(circuit_controlled)) | ||
self.log.debug('circuit: {}'.format(state_out_circuit)) | ||
|
||
f_mc = state_fidelity(state_out_exact, state_out_circuit) | ||
self.log.debug('The fidelity between matrix and circuit: {}\n=================='.format(f_mc)) | ||
# self.assertAlmostEqual(f_mc, 1) | ||
|
||
return | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |