Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translation perf tests (operator, circuit) #289

Merged
merged 6 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions tangelo/linq/tests/test_translator_perf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Copyright 2023 Good Chemistry Company.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Test class to keep track of performance of translator module, for format convertion """

import unittest
import os
import time

from itertools import product

from tangelo.linq import Gate, Circuit
from tangelo.toolboxes.operators import QubitOperator
from tangelo.helpers.utils import installed_backends
from tangelo.linq import translate_operator, translate_circuit
from tangelo.linq.translator.translate_qubitop import FROM_TANGELO as FROM_TANGELO_OP
from tangelo.linq.translator.translate_qubitop import TO_TANGELO as TO_TANGELO_OP
from tangelo.linq.translator.translate_circuit import FROM_TANGELO as FROM_TANGELO_C
from tangelo.linq.translator.translate_circuit import TO_TANGELO as TO_TANGELO_C


# Build artificially large operator made of all possible "full" Pauli words (no 'I') of length n_qubits
n_qubits_op = 10
n_terms = 3 ** n_qubits_op
terms = {tuple(zip(range(n_qubits_op), pw)): 1.0 for pw in product(['X', 'Y', 'Z'], repeat=n_qubits_op)}
tangelo_op = QubitOperator()
tangelo_op.terms = terms

# Build artificially large quantum circuit
n_qubits = 20
n_repeat = 4000
gates = [Gate('X', i) for i in range(n_qubits)] + [Gate("CNOT", (i+1) % n_qubits, control=i) for i in range(n_qubits)]
tangelo_c = Circuit(gates * n_repeat)


class PerfTranslatorTest(unittest.TestCase):

def test_perf_operator(self):
""" Performance test with a reasonable large input for operator """

print(f'\n[Performance Test :: linq operator format conversion]')
print(f'\tInput size: n_qubits={n_qubits_op}, n_terms={n_terms}\n')

for f in FROM_TANGELO_OP:
try:
tstart = time.time()
target_op = translate_operator(tangelo_op, source="tangelo", target=f)
print(f"\tFormat conversion from {'tangelo':12} to {f:12} :: {time.time()-tstart} s")
except Exception:
continue

if f in TO_TANGELO_OP:
try:
tstart = time.time()
translate_operator(target_op, source=f, target="tangelo")
print(f"\tFormat conversion from {f:12} to {'tangelo':12} :: {time.time()-tstart} s")
except Exception:
continue

def test_perf_circuit(self):
""" Performance test with a reasonable large input for quantum circuit """

print(f'\n[Performance Test :: linq circuit format conversion]')
print(f'\tInput size: n_qubits={tangelo_c.width}, n_gates={tangelo_c.size}\n')

for f in FROM_TANGELO_C:
try:
tstart = time.time()
target_c = translate_circuit(tangelo_c, source="tangelo", target=f)
print(f"\tFormat conversion from {'tangelo':12} to {f:12} :: {time.time()-tstart} s")
except Exception:
continue

if f in TO_TANGELO_C:
try:
tstart = time.time()
translate_circuit(target_c, source=f, target="tangelo")
print(f"\tFormat conversion from {f:12} to {'tangelo':12} :: {time.time()-tstart} s")
except Exception:
continue
26 changes: 0 additions & 26 deletions tangelo/linq/tests/test_translator_qubitop.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,32 +197,6 @@ def test_tangelo_to_qiskit_H2_eigenvalues(self):

np.testing.assert_array_almost_equal(eigenvalues_tangelo, eigenvalues_qiskit.eigenvalues)

@unittest.skipIf("qiskit" not in installed_backends, "Test Skipped: Qiskit not available \n")
def test_tangelo_to_qiskit_big(self):
"""Test translation from a tangelo to a qiskit operator, for a large input"""

n_qubits = 10
n_terms = 3**n_qubits

# Build large operator made of all possible "full" Pauli words (no 'I') of length n_qubits
terms = {tuple(zip(range(n_qubits), pw)): 1.0 for pw in product(['X', 'Y', 'Z'], repeat=n_qubits)}
q_op = QubitOperator()
q_op.terms = terms

s, t = "tangelo", "qiskit"
tstart1 = time.time()
tmp_op = translate_operator(q_op, source=s, target=t)
tstop1 = time.time()
print(f"Qubit operator conversion {s} to {t}: {tstop1 - tstart1:.1f} (terms = {n_terms})")

t, s = s, t
tstart2 = time.time()
q_op2 = translate_operator(tmp_op, source=s, target=t)
tstop2 = time.time()
print(f"Qubit operator conversion {s} to {t}: {tstop2 - tstart2:.1f} (terms = {n_terms})")

assert(q_op == q_op2)


if __name__ == "__main__":
unittest.main()