Skip to content

Commit

Permalink
Update test_uniform_superposition_gate.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Hirmay authored Jul 2, 2024
1 parent 7c88d84 commit b415c91
Showing 1 changed file with 8 additions and 48 deletions.
56 changes: 8 additions & 48 deletions test/python/circuit/test_uniform_superposition_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
from ddt import ddt, data

from qiskit import QuantumCircuit
from qiskit.quantum_info import Operator
from qiskit.quantum_info import Operator, Statevector
from qiskit.compiler import transpile

from qiskit.circuit.library.data_preparation import (
UniformSuperpositionGate,
)



@ddt
class TestUniformSuperposition(QiskitTestCase):
"""Test initialization with UniformSuperpositionGate class"""
Expand All @@ -41,10 +42,7 @@ def test_uniform_superposition_gate(self, num_superpos_states):
[1.0] * num_superpos_states + [0.0] * (2**n - num_superpos_states)
)
gate = UniformSuperpositionGate(num_superpos_states, n)
qc = QuantumCircuit(n)
qc.append(gate, list(range(n)))
unitary_matrix = np.array(Operator(qc).data)
actual_sv = unitary_matrix[:, 0]
actual_sv = Statevector(gate)
np.testing.assert_allclose(desired_sv, actual_sv)

@data(2, 3, 5, 13)
Expand All @@ -54,7 +52,7 @@ def test_inverse_uniform_superposition_gate(self, num_superpos_states):
gate = UniformSuperpositionGate(num_superpos_states, n)
qc = QuantumCircuit(n)
qc.append(gate, list(range(n)))
qc.append(gate.inverse(), list(range(n)))
qc.append(gate.inverse(annotated=True), list(range(n)))
actual_unitary_matrix = np.array(Operator(qc).data)
desired_unitary_matrix = np.eye(2**n)
np.testing.assert_allclose(desired_unitary_matrix, actual_unitary_matrix, atol=1e-14)
Expand All @@ -74,7 +72,7 @@ def test_incompatible_int_num_superpos_states_and_qubit_args(self, n):
with self.assertRaises(ValueError):
UniformSuperpositionGate(num_superpos_states, n)

@data(2, 3, 5, 13)
@data(2, 3, 5)
def test_extra_qubits(self, num_superpos_states):
"""Tests for cases where n >= log2(num_superpos_states)"""
num_extra_qubits = 2
Expand All @@ -83,57 +81,19 @@ def test_extra_qubits(self, num_superpos_states):
[1.0] * num_superpos_states + [0.0] * (2**n - num_superpos_states)
)
gate = UniformSuperpositionGate(num_superpos_states, n)
qc = QuantumCircuit(n)
qc.append(gate, list(range(n)))
unitary_matrix = np.array(Operator(qc).data)
actual_sv = unitary_matrix[:, 0]
actual_sv = Statevector(gate)
np.testing.assert_allclose(desired_sv, actual_sv)

@data(2, 3, 5, 13)
@data(2, 3, 5)
def test_no_qubit_args(self, num_superpos_states):
"""Test Uniform Superposition Gate without passing the number of qubits as an argument"""
n = int(math.ceil(math.log2(num_superpos_states)))
desired_sv = (1 / np.sqrt(num_superpos_states)) * np.array(
[1.0] * num_superpos_states + [0.0] * (2**n - num_superpos_states)
)
gate = UniformSuperpositionGate(num_superpos_states)
qc = QuantumCircuit(n)
qc.append(gate, list(range(n)))
unitary_matrix = np.array(Operator(qc).data)
actual_sv = unitary_matrix[:, 0]
np.testing.assert_allclose(desired_sv, actual_sv)

@data(2, 3, 5, 13)
def test_none_qubit_args(self, num_superpos_states):
"""Test Uniform Superposition Gate by setting the number of qubits as None"""
n = int(math.ceil(math.log2(num_superpos_states)))
desired_sv = (1 / np.sqrt(num_superpos_states)) * np.array(
[1.0] * num_superpos_states + [0.0] * (2**n - num_superpos_states)
)
num_qubits = None
gate = UniformSuperpositionGate(num_superpos_states, num_qubits)
qc = QuantumCircuit(n)
qc.append(gate, list(range(n)))
unitary_matrix = np.array(Operator(qc).data)
actual_sv = unitary_matrix[:, 0]
actual_sv = Statevector(gate)
np.testing.assert_allclose(desired_sv, actual_sv)

@data(2, 3, 5, 13)
def test_uniform_superposition_gate_transpile(self, num_superpos_states):
"""Test Uniform Superposition Gate to ensure that transpile operations (and
resulting unitaries) work as expected"""
n = int(math.ceil(math.log2(num_superpos_states)))
desired_sv = (1 / np.sqrt(num_superpos_states)) * np.array(
[1.0] * num_superpos_states + [0.0] * (2**n - num_superpos_states)
)
gate = UniformSuperpositionGate(num_superpos_states, n)
qc = QuantumCircuit(n)
qc.append(gate, list(range(n)))
qc = transpile(qc, basis_gates=["x", "h", "ry", "cx", "ch", "cry"], optimization_level=3)
unitary_matrix = np.array(Operator(qc).data)
actual_sv = unitary_matrix[:, 0]
np.testing.assert_allclose(desired_sv, actual_sv, atol=1e-14)


if __name__ == "__main__":
unittest.main()

0 comments on commit b415c91

Please sign in to comment.