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

Create glorious_folding_amplifier.py #36

Draft
wants to merge 34 commits into
base: main
Choose a base branch
from
Draft
Changes from 16 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
7911e36
Create glorious_folding_amplifier.py
mrvee-qC-bee Jan 24, 2023
af082ea
refactor: modify structure + add inverse copy
mrvee-qC-bee Jan 29, 2023
30ee3b6
refactor(folding_amplifier): make apply_folding a bit efficient
mrvee-qC-bee Jan 29, 2023
4230d01
docs(folding_amplifier): Add module docstring
mrvee-qC-bee Jan 29, 2023
415cb06
refactor(folding_amplifier): make apply_folding little bit efficient
mrvee-qC-bee Jan 29, 2023
2effe60
refactor(folding_amplifier): make apply_folding a bit memory efficient
mrvee-qC-bee Jan 31, 2023
9927cbc
refactor(folding_amplifier): isolate GloriousFoldingAmplifier class
mrvee-qC-bee Jan 31, 2023
624ff62
refactor(folding_amplifier): restructure GloriousFoldingAmplifier class
mrvee-qC-bee Feb 2, 2023
c074e01
docs(folding_amplifier): fix grammar
mrvee-qC-bee Feb 2, 2023
8ae67b7
docs(folding_amplifier): Add TODO
mrvee-qC-bee Feb 2, 2023
2742079
refactor(folding_amplifier): add general typing for noise_factor
mrvee-qC-bee Feb 2, 2023
68583f2
docs(folding_amplifier): add better exception message
mrvee-qC-bee Feb 2, 2023
2303465
refactor(folding_amplifier): make invert function glorious
mrvee-qC-bee Feb 3, 2023
78dc662
docs(folding_amplifier): make docs a bit nicer
mrvee-qC-bee Feb 5, 2023
f3e03b4
refactor(folding_amplifier): refactor typing and exception handling
mrvee-qC-bee Feb 5, 2023
746afd4
docs(folding_amplifier): fix grammar
mrvee-qC-bee Feb 5, 2023
efaa007
docs(folding_amplifier): make spacing consistent
mrvee-qC-bee Feb 5, 2023
63fd450
refactor(folding_amplifier): refactor typing and exception handling
mrvee-qC-bee Feb 5, 2023
1329252
docs(folding_amplifier): rename for distinction
mrvee-qC-bee Feb 5, 2023
3401b1f
docs(folding_amplifier): rename for distinction
mrvee-qC-bee Feb 5, 2023
73ab3d6
docs(folding_amplifier): fix lint and typos
mrvee-qC-bee Feb 7, 2023
ff175fe
feat(folding_amplifier): add glorious local folding amplifier
mrvee-qC-bee Feb 7, 2023
c66f2aa
refactor(folding_amplifier): improve validate_gates_to_fold
mrvee-qC-bee Feb 14, 2023
98f0039
refactor(folding_amplifier): add glorious_folding_amplifier interface
mrvee-qC-bee Feb 23, 2023
a2575ff
refactor(folding_amplifier): clean glorious_global_folding amplifier
mrvee-qC-bee Mar 12, 2023
2689957
docs(folding_amplifier): make error messages concise
mrvee-qC-bee Mar 12, 2023
1baf3f1
docs(folding_amplifier): remove typing from docstring
mrvee-qC-bee Mar 12, 2023
b569ea3
refactor(folding_amplifier): add naive support for partial folding
mrvee-qC-bee Apr 10, 2023
b1fb40d
docs(folding_amplifier): add doc strings to newer functions
mrvee-qC-bee Apr 10, 2023
aa77062
docs(folding_amplifier): remove comments
mrvee-qC-bee Apr 10, 2023
cf5b96c
refactor(folding_amplifier): draft masking function to be ported
mrvee-qC-bee Apr 11, 2023
5bc8afb
refactor(folding_amplifier): make globalfolding refactor functional
mrvee-qC-bee May 6, 2023
7db66a7
Update zne/noise_amplification/folding_amplifier/glorious_folding_amp…
mrvee-qC-bee May 16, 2023
31054ce
feat(glorius_folding_amplifier): fix imports and inits
mrvee-qC-bee May 28, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2022-2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Glorious DAG Folding Noise Amplification (Temporary)"""

import copy

from qiskit.circuit.library import Barrier
from qiskit.dagcircuit import DAGCircuit

from ..noise_amplifier import DAGNoiseAmplifier


class GloriousFoldingAmplifier(DAGNoiseAmplifier):
"""Alternatingly composes the circuit and its inverse as many times as indicated
by the ``noise_factor``.

References:
[1] T. Giurgica-Tiron et al. (2020).
Digital zero noise extrapolation for quantum error mitigation.
`<https://ieeexplore.ieee.org/document/9259940>`
"""

def amplify_dag_noise(self, dag: DAGCircuit, noise_factor: float) -> DAGCircuit:
"""Applies global folding to input DAGCircuit and returns amplified circuit"""
num_full_foldings = self._compute_folding_nums(noise_factor)
return self._apply_full_folding(dag, num_full_foldings)

mrvee-qC-bee marked this conversation as resolved.
Show resolved Hide resolved
def _apply_full_folding(
self,
dag: DAGCircuit,
num_foldings: int,
) -> DAGCircuit:
"""Fully folds the original DAG circuit a number of ``num_foldings`` times.

Args:
dag (DAGCircuit): The original dag circuit without foldings.
num_foldings (float): Number of times the circuit should be folded.

Returns:
DAGCircuit: The noise amplified DAG circuit.
"""
barrier = Barrier(dag.num_qubits())
inverse_dag = self._invert_dag(dag)
noisy_dag = copy.deepcopy(dag)
for _ in range(num_foldings):
noisy_dag.apply_operation_back(barrier, qargs=noisy_dag.qubits)
noisy_dag.compose(inverse_dag, inplace=True)
noisy_dag.apply_operation_back(barrier, qargs=noisy_dag.qubits)
noisy_dag.compose(dag, inplace=True)
# TODO: noisy_dag.apply_operation_back(barrier, qargs=noisy_dag.qubits)
return noisy_dag

mrvee-qC-bee marked this conversation as resolved.
Show resolved Hide resolved
def _invert_dag(self, dag_to_inverse: DAGCircuit) -> DAGCircuit:
"""Inverts an input dag circuit.

Args:
dag_to_inverse (DAGCircuit) : The original dag circuit to invert.

Returns:
DAGCircuit: The inverted DAG circuit.
"""
inverted_dag = dag_to_inverse.copy_empty_like()
for node in dag_to_inverse.topological_op_nodes():
inverted_dag.apply_operation_front(
node.op.inverse(), qargs=node.qargs, cargs=node.cargs
)
return inverted_dag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUPER COOL!! You wrote this using only the DAG API, which makes the code more efficient and, ready to be contributed to Terra!! Let's take care of this in our next chat 🥳


def _validate_noise_factor(self, noise_factor: float) -> float:
"""Normalizes and validates noise factor.
Args:
noise_factor (float) : The original noisefactor input.

Returns:
float: Normalised noisefactor input.

Raises:
ValueError: If input noise_factor value is not of type float.
"""
try:
noise_factor = float(noise_factor)
mrvee-qC-bee marked this conversation as resolved.
Show resolved Hide resolved
except ValueError as exc:
raise ValueError(
f"`noise_factor` is expected to be a positive floating value. "
f"Received value of {noise_factor} instead."
) from exc
mrvee-qC-bee marked this conversation as resolved.
Show resolved Hide resolved
if noise_factor < 1:
raise ValueError(
f"`noise_factor` is expected to be a positive float noise_factor >= 1."
f"Received {noise_factor} instead."
)
if noise_factor % 2 == 0:
raise ValueError(
f"`noise_factor` is expected to be a positive odd noise_factor. "
f"Received {noise_factor} instead."
)
return noise_factor

def _compute_folding_nums(self, noise_factor: float) -> int:
mrvee-qC-bee marked this conversation as resolved.
Show resolved Hide resolved
"""Compute num foldings.
Args:
noise_factor (float) : The original noise_factor input.

Returns:
int: Number of foldings calculated from noise_factor.
"""
noise_factor = self._validate_noise_factor(noise_factor)
mrvee-qC-bee marked this conversation as resolved.
Show resolved Hide resolved
return int((noise_factor - 1) / 2)