microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
iadavis/qiskit2-explore

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/pip/qsharp/utils/_utils.py

49lines · modecode

1# Copyright (c) Microsoft Corporation.
2# Licensed under the MIT License.
3
4from .._qsharp import run
5from typing import List
6import math
7
8
9def dump_operation(operation: str, num_qubits: int) -> List[List[complex]]:
10 """
11 Returns a square matrix of complex numbers representing the operation performed.
12
13 :param operation: The operation to be performed, which must operate on a list of qubits.
14 :param num_qubits: The number of qubits to be used.
15
16 :returns: The matrix representing the operation.
17 """
18 code = f"""{{
19 let op = {operation};
20 use (targets, extra) = (Qubit[{num_qubits}], Qubit[{num_qubits}]);
21 for i in 0..{num_qubits}-1 {{
22 H(targets[i]);
23 CNOT(targets[i], extra[i]);
24 }}
25 operation ApplyOp (op : (Qubit[] => Unit), targets : Qubit[]) : Unit {{ op(targets); }}
26 ApplyOp(op, targets);
27 Microsoft.Quantum.Diagnostics.DumpMachine();
28 ResetAll(targets + extra);
29 }}"""
30 result = run(code, shots=1, save_events=True)[0]
31 state = result["events"][-1].state_dump().get_dict()
32 num_entries = pow(2, num_qubits)
33 factor = math.sqrt(num_entries)
34 ndigits = 6
35 matrix = []
36 for i in range(num_entries):
37 matrix += [[]]
38 for j in range(num_entries):
39 entry = state.get(i * num_entries + j)
40 if entry is None:
41 matrix[i] += [complex(0, 0)]
42 else:
43 matrix[i] += [
44 complex(
45 round(factor * entry.real, ndigits),
46 round(factor * entry.imag, ndigits),
47 )
48 ]
49 return matrix
50