Coverage for /home/runner/work/tket/tket/pytket/pytket/utils/prepare.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-03-14 11:30 +0000

1# Copyright Quantinuum 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15from pytket.circuit import Circuit 

16from pytket.passes import ContextSimp 

17from pytket.transform import separate_classical 

18 

19 

20def prepare_circuit( 

21 circ: Circuit, allow_classical: bool = True, xcirc: Circuit | None = None 

22) -> tuple[Circuit, Circuit]: 

23 """ 

24 Prepare a circuit for processing by a backend device. 

25 

26 This method first makes all inputs into Create operations (assuming an initial all- 

27 zero state) and all outputs into Discard operations (so that the circuit can no 

28 longer be usefully extended or appended to another circuit). It then attempts to 

29 apply various simplifications that take advantage of the known initial state and the 

30 fact that any unmeasured state is discarded. Finally, it separates the circuit into 

31 two circuits, the first of which is to be run on the backend (after any further 

32 compilation has been applied), and the second of which is a pure-classical circuit 

33 (on the same bits) which encodes classical post-processing of the measurement 

34 results. This post-processing is applied automatically when you pass the classical 

35 circuit as the `ppcirc` argument to `BackendResult.get_counts()` or 

36 `BackendResult.get_shots()`. 

37 

38 The original circuit is not modified by this method. 

39 

40 :param circ: input circuit 

41 :param allow_classical: allow insertion of mid-circuit classical operations? 

42 :param xcirc: 1-qubit circuit implementing an X gate in the transformed circuit (if 

43 omitted, an X gate is used) 

44 :return: (c0, ppcirc) where c0 is the simplified circuit and ppcirc should be passed 

45 to `BackendResult.get_counts()` or `BackendResult.get_shots()` when retrieving 

46 the final results. 

47 """ 

48 c = circ.copy() 

49 c.qubit_create_all() 

50 c.qubit_discard_all() 

51 ContextSimp(allow_classical, xcirc).apply(c) 

52 return separate_classical(c)