def test_qft_mutability(self): """Test the mutability of the QFT circuit.""" qft = QFT() with self.subTest(msg='empty initialization'): self.assertEqual(qft.num_qubits, 0) self.assertEqual(qft.data, []) with self.subTest(msg='changing number of qubits'): qft.num_qubits = 3 self.assertQFTIsCorrect(qft, num_qubits=3) with self.subTest(msg='test diminishing the number of qubits'): qft.num_qubits = 1 self.assertQFTIsCorrect(qft, num_qubits=1) with self.subTest(msg='test with swaps'): qft.num_qubits = 4 qft.do_swaps = False self.assertQFTIsCorrect(qft, add_swaps_at_end=True) with self.subTest(msg='set approximation'): qft.approximation_degree = 2 qft.do_swaps = True with self.assertRaises(AssertionError): self.assertQFTIsCorrect(qft)
def test_warns_if_too_large(self): """Test that a warning is issued if the user tries to make a circuit that would need to represent angles smaller than the smallest normal double-precision floating-point number. It's too slow to actually let QFT construct a 1050+ qubit circuit for such a simple test, so we temporarily prevent QuantumCircuits from being created in order to short-circuit the QFT builder.""" class SentinelException(Exception): """Dummy exception that raises itself as soon as it is created.""" def __init__(self, *_args, **_kwargs): super().__init__() raise self # We don't want to issue a warning on mutation until we know that the values are # finalised; this is because a user might want to mutate the number of qubits and the # approximation degree. In these cases, wait until we try to build the circuit. with warnings.catch_warnings(record=True) as caught_warnings: warnings.filterwarnings( "always", category=RuntimeWarning, module=r"qiskit\..*", message=r".*precision loss in QFT.*", ) qft = QFT() # Even with the approximation this will trigger the warning. qft.num_qubits = 1080 qft.approximation_degree = 20 self.assertFalse(caught_warnings) # Short-circuit the build method so it exits after input validation, but without actually # spinning the CPU to build a huge, useless object. with unittest.mock.patch("qiskit.circuit.QuantumCircuit.__init__", SentinelException): with self.assertWarnsRegex(RuntimeWarning, "precision loss in QFT"): with self.assertRaises(SentinelException): qft._build()