Skip to content

Commit

Permalink
modify tests and create a new folder
Browse files Browse the repository at this point in the history
  • Loading branch information
korgan00 committed Nov 28, 2024
1 parent b9344d1 commit b1e5e1a
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# pylint: disable=import-error, invalid-name
""" Fixtures for tests """
import os

from pytest import fixture
Expand All @@ -9,8 +11,8 @@
)


@fixture(scope="session")
def serverless_client(self):
@fixture(scope="module")
def serverless_client():
"""Fixture for testing files."""
compose = DockerCompose(
resources_path,
Expand Down
9 changes: 9 additions & 0 deletions tests/docker/source_files/circuit_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from qiskit import QuantumCircuit


def create_hello_world_circuit() -> QuantumCircuit:
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()
return circuit
10 changes: 10 additions & 0 deletions tests/docker/source_files/consume_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import os
import tarfile
from qiskit_serverless import save_result

with tarfile.open("/data/my_file.tar", "r:gz") as tar:
with tar.extractfile("./my_file.txt") as f:
text = f.read()

print(text)
save_result({"Message": str(text)})
Binary file added tests/docker/source_files/data.tar
Binary file not shown.
1 change: 1 addition & 0 deletions tests/docker/source_files/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello World!")
22 changes: 22 additions & 0 deletions tests/docker/source_files/pattern.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler as Sampler

from qiskit_serverless import save_result

# all print statement will be available in job logs
print("Running pattern...")

# creating circuit
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()

# running Sampler primitive
sampler = Sampler()
quasi_dists = sampler.run([(circuit)]).result()[0].data.meas.get_counts()

# saves results of program execution,
# which will be accessible by calling `.result()`
save_result(quasi_dists)
print("Completed running pattern.")
20 changes: 20 additions & 0 deletions tests/docker/source_files/pattern_with_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# source_files/program_3.py

from qiskit_serverless import get_arguments, save_result

from qiskit.primitives import StatevectorSampler as Sampler

# get all arguments passed to this program
arguments = get_arguments()

# get specific argument that we are interested in
circuit = arguments.get("circuit")

sampler = Sampler()

quasi_dists = sampler.run([(circuit)]).result()[0].data.meas.get_counts()

print(f"Quasi distribution: {quasi_dists}")

# saving results of a program
save_result({"quasi_dists": quasi_dists})
13 changes: 13 additions & 0 deletions tests/docker/source_files/pattern_with_dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# source_files/program_4.py

from qiskit_serverless import save_result

import pendulum

dt_toronto = pendulum.datetime(2012, 1, 1, tz="America/Toronto")
dt_vancouver = pendulum.datetime(2012, 1, 1, tz="America/Vancouver")

diff = dt_vancouver.diff(dt_toronto).in_hours()

print(diff)
save_result({"hours": diff})
26 changes: 26 additions & 0 deletions tests/docker/source_files/pattern_with_parallel_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# source_files/program_with_parallel_workflow.py

from qiskit_serverless import get_arguments, save_result, distribute_task, get

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler as Sampler
from qiskit.circuit.random import random_circuit


@distribute_task()
def distributed_sample(circuit: QuantumCircuit):
"""Distributed task that returns quasi distribution for given circuit."""
return Sampler().run([(circuit)]).result()[0].data.meas.get_counts()


arguments = get_arguments()
circuits = arguments.get("circuits")

# run distributed tasks as async function
# we get task references as a return type
sample_task_references = [distributed_sample([(circuit)]) for circuit in circuits]

# now we need to collect results from task references
results = get(sample_task_references)

save_result({"results": results})
11 changes: 11 additions & 0 deletions tests/docker/source_files/produce_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import os
import tarfile
from qiskit_serverless import save_result

with open("./my_file.txt", "w") as f:
f.write("Hello!")

with tarfile.open("/data/my_file.tar", "w:gz") as tar:
tar.add("./my_file.txt")

save_result({"Message": "my_file.txt archived into my_file.tar"})
22 changes: 22 additions & 0 deletions tests/docker/source_files/program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler as Sampler

from qiskit_serverless import save_result

# all print statement will be available in job logs
print("Running program...")

# creating circuit
circuit = QuantumCircuit(2)
circuit.h(0)
circuit.cx(0, 1)
circuit.measure_all()

# running Sampler primitive
sampler = Sampler()
quasi_dists = sampler.run([(circuit)]).result()[0].data.meas.get_counts()

# saves results of program execution,
# which will be accessible by calling `.result()`
save_result(quasi_dists)
print("Completed running program.")
1 change: 1 addition & 0 deletions tests/basic/test_docker.py → tests/docker/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)


@mark.skip(reason="Speedup tests")
class TestFunctionsDocker:
"""Test class for integration testing with docker."""

Expand Down
File renamed without changes.
3 changes: 1 addition & 2 deletions tests/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ commands =
skip_install = true
commands =
black --diff --check .
pylint -rn basic/test_docker
pylint -rn experimental/test_docker_experimental
pylint -rn docker/*

[testenv:black]
skip_install = true
Expand Down

0 comments on commit b1e5e1a

Please sign in to comment.