Skip to content

Commit

Permalink
ucr comparison test
Browse files Browse the repository at this point in the history
  • Loading branch information
israelferrazaraujo committed Jan 5, 2024
1 parent bb9cba5 commit 1346a96
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Empty file added test/embedding/util/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions test/embedding/util/test_ucr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2023 qdna-lib project.

# 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.

"""
Tests for the ae_feature_map.py module.
"""

from unittest import TestCase
import numpy as np
from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.circuit.library import RZGate
from qdna.embedding.util.ucr import ucr

# pylint: disable=missing-function-docstring
# pylint: disable=missing-class-docstring


class TestUcr(TestCase):

def test_comparison(self):
n_ctrl_qubits = 2

target_qubit = n_ctrl_qubits
control_qubits = list(range(n_ctrl_qubits))

angles = np.random.rand(2**n_ctrl_qubits) * np.pi

# Uniformly controlled rotation
ucr_circuit = QuantumCircuit(n_ctrl_qubits+1)
ucry = ucr(RZGate, angles)
ucr_circuit.compose(ucry, [target_qubit, *control_qubits], inplace=True)

# Sequence of multicontrolled rotations
mcr_circuit = QuantumCircuit(n_ctrl_qubits+1)
mcr_00 = RZGate(angles[0]).control(num_ctrl_qubits=n_ctrl_qubits, ctrl_state='00')
mcr_01 = RZGate(angles[1]).control(num_ctrl_qubits=n_ctrl_qubits, ctrl_state='01')
mcr_10 = RZGate(angles[2]).control(num_ctrl_qubits=n_ctrl_qubits, ctrl_state='10')
mcr_11 = RZGate(angles[3]).control(num_ctrl_qubits=n_ctrl_qubits, ctrl_state='11')
mcr_circuit.compose(mcr_00, [*control_qubits, target_qubit], inplace=True)
mcr_circuit.compose(mcr_01, [*control_qubits, target_qubit], inplace=True)
mcr_circuit.compose(mcr_10, [*control_qubits, target_qubit], inplace=True)
mcr_circuit.compose(mcr_11, [*control_qubits, target_qubit], inplace=True)

# Compare
ucr_op = Operator(ucr_circuit).data
mcr_op = Operator(mcr_circuit).data

self.assertTrue(np.allclose(ucr_op, mcr_op))

0 comments on commit 1346a96

Please sign in to comment.