Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
33dd11b
add functional rotations base class
Cryoris Apr 1, 2020
cca3170
refactor first arithmetic circuits
Cryoris Apr 1, 2020
bcbad39
integrate polynomial rotations
Cryoris Apr 1, 2020
75d33b2
update inits
Cryoris Apr 1, 2020
14d6c35
add tests
Cryoris Apr 1, 2020
dfdda6c
skip test depending on Aqua
Cryoris Apr 1, 2020
44e4506
Merge branch 'master' into library-arithmetic
Cryoris Apr 1, 2020
2472e5f
fix imports: no weighted_adder, add functional_rot
Cryoris Apr 1, 2020
b8567ef
move circ diag in parsed-literal block
Cryoris Apr 1, 2020
e0176cd
adjust formatting of linear circ diag
Cryoris Apr 1, 2020
a9b6fe7
update rendering of docstrings
Cryoris Apr 1, 2020
ebcf7c0
move AND/OR gates from Aqua
Cryoris Apr 1, 2020
1e59509
use data getter not _data in copy
Cryoris Apr 1, 2020
a528d92
add missing gate inits
Cryoris Apr 1, 2020
e0d7ad9
fix imports
Cryoris Apr 1, 2020
52574e6
fix lint
Cryoris Apr 2, 2020
2dbed9b
Merge branch 'master' into library-arithmetic
Cryoris Apr 2, 2020
ef8adc2
move WSO from aqua
Cryoris Apr 3, 2020
4645b35
rename to weighted adder
Cryoris Apr 3, 2020
ef8a8b4
refactor weighted adder and make a circuit
Cryoris Apr 3, 2020
c4ca57a
updates inits with weighted adder
Cryoris Apr 3, 2020
4c72a27
add tests for weighted adder
Cryoris Apr 3, 2020
dde459b
Merge branch 'library-arithmetic' of github.com:Cryoris/qiskit-terra …
Cryoris Apr 3, 2020
1285864
Merge branch 'master' into library-arithmetic
Cryoris Apr 3, 2020
e1eed60
fix outdated mcu1 import
Cryoris Apr 3, 2020
012b92d
Merge branch 'master' into library-arithmetic
Cryoris Apr 5, 2020
5f7a9c9
add functional pauli rotations import
Cryoris Apr 6, 2020
0ab26aa
Update test/python/circuit/test_library.py
Cryoris Apr 6, 2020
f9fb70b
Update qiskit/circuit/library/arithmetic/weighted_adder.py
Cryoris Apr 6, 2020
7f5c9c7
Merge branch 'library-arithmetic' of github.com:Cryoris/qiskit-terra …
Cryoris Apr 6, 2020
47966ab
add comment on default behaviour of the weights
Cryoris Apr 6, 2020
76c3917
fix rst rendering
Cryoris Apr 6, 2020
19ce135
include arithmetic circs in sphinx
Cryoris Apr 6, 2020
c754fcd
fix copyright
Cryoris Apr 6, 2020
cf45675
add _invalidate() method
Cryoris Apr 6, 2020
0e3b792
Merge branch 'master' into library-arithmetic
Cryoris Apr 6, 2020
a3ad68a
Merge branch 'master' into library-arithmetic
mergify[bot] Apr 7, 2020
bd2662e
Merge branch 'master' into library-arithmetic
ajavadia Apr 7, 2020
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
42 changes: 41 additions & 1 deletion qiskit/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
# (C) Copyright IBM 2017, 2020.
#
# 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
Expand All @@ -28,7 +28,47 @@
InnerProduct
Permutation
XOR


Arithmetic Circuits
===================

Functional Pauli Rotations
++++++++++++++++++++++++++

.. autosummary::
:toctree: ../stubs/

FunctionalPauliRotations
LinearPauliRotations
PolynomialPauliRotations
PiecewiseLinearPauliRotations

Adders
++++++

.. autosummary::
:toctree: ../stubs/

WeightedAdder

Comparators
+++++++++++

.. autosummary::
:toctree: ../stubs/

IntegerComparator

"""


from .boolean_logic import Permutation, XOR, InnerProduct
from .arithmetic import (
FunctionalPauliRotations,
LinearPauliRotations,
PiecewiseLinearPauliRotations,
PolynomialPauliRotations,
IntegerComparator,
WeightedAdder
)
22 changes: 22 additions & 0 deletions qiskit/circuit/library/arithmetic/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2020.
#
# 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.

"""The arithmetic circuit library."""

from .functional_pauli_rotations import FunctionalPauliRotations
from .integer_comparator import IntegerComparator
from .linear_pauli_rotations import LinearPauliRotations
from .piecewise_linear_pauli_rotations import PiecewiseLinearPauliRotations
from .polynomial_pauli_rotations import PolynomialPauliRotations
from .weighted_adder import WeightedAdder
Comment thread
Cryoris marked this conversation as resolved.
141 changes: 141 additions & 0 deletions qiskit/circuit/library/arithmetic/functional_pauli_rotations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2020.
#
# 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.

"""Base class for functional Pauli rotations."""

from typing import Optional, List, Tuple

from abc import ABC, abstractmethod
from qiskit.circuit import QuantumCircuit, Qubit, Clbit, Instruction


class FunctionalPauliRotations(QuantumCircuit, ABC):
"""Base class for functional Pauli rotations."""

def __init__(self,
num_state_qubits: Optional[int] = None,
basis: str = 'Y',
name: str = 'F') -> None:
r"""Create a new functional Pauli rotation circuit.

Args:
num_state_qubits: The number of qubits representing the state :math:`|x\rangle`.
basis: The kind of Pauli rotation to use. Must be 'X', 'Y' or 'Z'.
name: The name of the circuit object.
"""
super().__init__(name=name)

# define internal parameters
self._num_state_qubits = None
self._basis = None

# store parameters
self.num_state_qubits = num_state_qubits
self.basis = basis

@property
def basis(self) -> str:
"""The kind of Pauli rotation to be used.

Set the basis to 'X', 'Y' or 'Z' for controlled-X, -Y, or -Z rotations respectively.

Returns:
The kind of Pauli rotation used in controlled rotation.
"""
return self._basis

@basis.setter
def basis(self, basis: str) -> None:
"""Set the kind of Pauli rotation to be used.

Args:
basis: The Pauli rotation to be used.

Raises:
ValueError: The provided basis in not X, Y or Z.
"""
basis = basis.lower()
if self._basis is None or basis != self._basis:
if basis not in ['x', 'y', 'z']:
raise ValueError('The provided basis must be X, Y or Z, not {}'.format(basis))
self._invalidate()
self._basis = basis

@property
def num_state_qubits(self) -> int:
r"""The number of state qubits representing the state :math:`|x\rangle`.

Returns:
The number of state qubits.
"""
return self._num_state_qubits

@num_state_qubits.setter
def num_state_qubits(self, num_state_qubits: Optional[int]) -> None:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why is the setter input optional?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

So that the circuit can be "reset" by

circuit.num_state_qubits = None

Maybe an application checks of the number of qubits has been set or not, though I don't have a concrete example where this would be needed. Should I set it to not support None?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

that's fine. i just see that in this PR some setters accept None and some don't, is that intentional?

"""Set the number of state qubits.

Note that this may change the underlying quantum register, if the number of state qubits
changes.

Args:
num_state_qubits: The new number of qubits.
"""
if self._num_state_qubits is None or num_state_qubits != self._num_state_qubits:
self._invalidate()
self._num_state_qubits = num_state_qubits

self._reset_registers(num_state_qubits)

def _invalidate(self) -> None:
"""Invalidate the current build of the circuit."""
self._data = None

@abstractmethod
def _reset_registers(self, num_state_qubits: Optional[int]) -> None:
"""Reset the registers according to the new number of state qubits.

Args:
num_state_qubits: The new number of qubits.
"""
raise NotImplementedError

@property
def num_ancilla_qubits(self) -> int:
"""The minimum number of ancilla qubits in the circuit.

Returns:
The minimal number of ancillas required.
"""
return 0

@abstractmethod
def _configuration_is_valid(self, raise_on_failure: bool = True) -> bool:
raise NotImplementedError

@abstractmethod
def _build(self):
# if data is populated we already built the circuit
if self._data:
return

# check if the current configuration is valid and re-initiate the circuit data
_ = self._configuration_is_valid()
self._data = []

@property
def data(self) -> List[Tuple[Instruction, List[Qubit], List[Clbit]]]:
"""Get the circuit definition."""
if self._data is None:
self._build()
return super().data
Loading