Skip to content

Add a helper to get action of an operation on a state#3300

Open
fedimser wants to merge 6 commits into
mainfrom
fedimser/test-utils-2
Open

Add a helper to get action of an operation on a state#3300
fedimser wants to merge 6 commits into
mainfrom
fedimser/test-utils-2

Conversation

@fedimser

@fedimser fedimser commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

This addresses #3296

See https://github.com/microsoft/qdk/blob/fedimser/test-utils-1/tmp/Demo.ipynb for examples how it can be used to test more complex operations using Q# wrappers.

Usage example without Context:

from qdk import qsharp, code
from qdk.test_utils import dump_operation_on_state

qsharp.eval("""
operation MyOp1(q: Qubit[]) : Unit {
    H(q[0]);
    CNOT(q[0], q[1]);
    Z(q[1]);
}
""")

dump_operation_on_state(code.MyOp1, num_qubits=2)
>>> [(0.7071067811865476+0j), 0.0, 0.0, (-0.7071067811865476-0j)]
dump_operation_on_state(code.MyOp1, num_qubits=2, initial_state=[0,1,0,0])
>>> [0.0, (-0.7071067811865476-0j), (0.7071067811865476+0j), 0.0]

Usage example with Context:

import qdk
from qdk.test_utils import dump_operation_on_state

ctx = qdk.Context()
ctx.eval("""
operation MyOp1(q: Qubit[]) : Unit {
    H(q[0]);
    CNOT(q[0], q[1]);
    Z(q[1]);
}
""")

dump_operation_on_state(ctx.code.MyOp1, num_qubits=2, context=ctx)
>>> [(0.7071067811865476+0j), 0.0, 0.0, (-0.7071067811865476-0j)]

Comment thread source/qdk_package/qdk/_test_helper.py Fixed
Comment thread source/qdk_package/tests/test_test_helper.py Fixed
Comment thread source/qdk_package/tests/test_test_helper.py Fixed
@fedimser fedimser marked this pull request as ready for review June 9, 2026 22:18
@fedimser fedimser force-pushed the fedimser/test-utils-2 branch from 153c75f to 849737d Compare June 12, 2026 20:09
@fedimser fedimser force-pushed the fedimser/test-utils-2 branch from 849737d to db21f63 Compare June 12, 2026 20:09
Comment thread source/qdk_package/qdk/test_utils.py Fixed
Comment thread source/qdk_package/tests/test_test_utils.py Dismissed
Comment thread source/qdk_package/tests/test_test_utils.py Dismissed
@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for 15bca19

Test This Branch On Main Difference
compile core + standard lib 24614955 bytes 24591003 bytes 23952 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for 15c9cf2

Test This Branch On Main Difference
compile core + standard lib 24614043 bytes 24591003 bytes 23040 bytes

@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for 4163a8f

Test This Branch On Main Difference
compile core + standard lib 24614035 bytes 24591003 bytes 23032 bytes

@fedimser fedimser changed the title Add a helper to get action of an operation on a zero state Add a helper to get action of an operation on a state Jun 12, 2026
@github-actions

Copy link
Copy Markdown

Change in memory usage detected by benchmark.

Memory Report for a9a7ec4

Test This Branch On Main Difference
compile core + standard lib 24614035 bytes 24591003 bytes 23032 bytes

if initial_state is None:
initial_state = [1.0] # |00..0> state.
if type(op) is str:
op = context.eval(op)
Comment thread source/qdk_package/qdk/test_utils.py Dismissed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants