From b415c910c3ee4cf5470fdc7d5d70aa1af451e373 Mon Sep 17 00:00:00 2001 From: Hirmay Sandesara <56473003+Hirmay@users.noreply.github.com> Date: Tue, 2 Jul 2024 15:03:13 +0530 Subject: [PATCH] Update test_uniform_superposition_gate.py --- .../test_uniform_superposition_gate.py | 56 +++---------------- 1 file changed, 8 insertions(+), 48 deletions(-) diff --git a/test/python/circuit/test_uniform_superposition_gate.py b/test/python/circuit/test_uniform_superposition_gate.py index 7df38cf3aaf..a578cc7f154 100644 --- a/test/python/circuit/test_uniform_superposition_gate.py +++ b/test/python/circuit/test_uniform_superposition_gate.py @@ -21,7 +21,7 @@ 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 ( @@ -29,6 +29,7 @@ ) + @ddt class TestUniformSuperposition(QiskitTestCase): """Test initialization with UniformSuperpositionGate class""" @@ -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) @@ -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) @@ -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 @@ -83,13 +81,10 @@ 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))) @@ -97,43 +92,8 @@ def test_no_qubit_args(self, num_superpos_states): [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()