Skip to content

Commit

Permalink
resolve flaky mirror QV circuit test (#2127)
Browse files Browse the repository at this point in the history
* simplify mirror QV circuit generation

Co-authored-by: Misty Wahl <[email protected]>

* ensure `mypy` knows qv_circuit is `cirq.Circuit`

Co-authored-by: Misty Wahl <[email protected]>

* test for approximate equality in equal circuits

this is needed since the `cirq.inverse` function can introduce tiny
deviations (on the order of 1e-15) when computing the inverses of matrix
gates. exact equality is awesome, but some numerical inacuracy here
seems inevitable

---------

Co-authored-by: Misty Wahl <[email protected]>
  • Loading branch information
natestemen and Misty-W authored Dec 14, 2023
1 parent 0a9c202 commit d0fd198
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
20 changes: 7 additions & 13 deletions mitiq/benchmarks/mirror_qv_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"""Functions to create a Mirror Quantum Volume Benchmarking circuit
as defined in https://arxiv.org/abs/2303.02108."""

from typing import Optional
from typing import Optional, cast

import cirq

from mitiq import QPROGRAM
from mitiq.benchmarks.quantum_volume_circuits import (
generate_quantum_volume_circuit,
)
from mitiq.interface.conversions import convert_from_mitiq, convert_to_mitiq
from mitiq.interface.conversions import convert_from_mitiq


def generate_mirror_qv_circuit(
Expand Down Expand Up @@ -60,22 +60,16 @@ def generate_mirror_qv_circuit(
else:
first_half_depth = int((depth + 1) / 2)

qv_generated, _ = generate_quantum_volume_circuit(
qv_circuit, _ = generate_quantum_volume_circuit(
num_qubits, first_half_depth, seed=seed, decompose=decompose
)
qv_half_circ, _ = convert_to_mitiq(qv_generated)
qv_circuit = cast(cirq.Circuit, qv_circuit)

mirror_half_circ = cirq.Circuit()
qv_half_ops = list(qv_half_circ.all_operations())
for i in range(len(qv_half_ops))[::-1]:
op_inverse = cirq.inverse(qv_half_ops[i])
mirror_half_circ.append(op_inverse, strategy=cirq.InsertStrategy.NEW)

circ = qv_half_circ + mirror_half_circ
mirror_qv_circuit = qv_circuit + cirq.inverse(qv_circuit)

if decompose:
# Decompose random unitary gates into simpler gates.
circ = cirq.Circuit(cirq.decompose(circ))
mirror_qv_circuit = cirq.Circuit(cirq.decompose(mirror_qv_circuit))

return_type = "cirq" if not return_type else return_type
return convert_from_mitiq(circ, return_type)
return convert_from_mitiq(mirror_qv_circuit, return_type)
2 changes: 1 addition & 1 deletion mitiq/benchmarks/tests/test_mirror_qv_circuits.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_generate_model_circuit_with_seed():
circuit_2 = generate_mirror_qv_circuit(4, 3, seed=1)
circuit_3 = generate_mirror_qv_circuit(4, 3, seed=2)

assert circuit_1 == circuit_2
assert cirq.approx_eq(circuit_1, circuit_2, atol=1e-12)
assert circuit_2 != circuit_3


Expand Down

0 comments on commit d0fd198

Please sign in to comment.