Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

CircuitSampler fails on expressions consisting only of OperatorStateFns #1321

Closed
hay-k opened this issue Oct 8, 2020 · 4 comments
Closed
Assignees

Comments

@hay-k
Copy link
Contributor

hay-k commented Oct 8, 2020

Information

  • Qiskit Aqua version: Latest source code from github

What is the current behavior?

When the convert method of CircuitSampler at aqua/operators/converters/circuit_sampler.py is called with such an operator which neither contains any CircuitStateFn, nor any of the contained operators can be converted to CircuitStateFn, the following error is raised

Traceback (most recent call last):
  File "D:/qiskit advocate/test_project/main.py", line 9, in <module>
    sampled_op = sampler.convert(circuit_expectation)
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\aqua\operators\converters\circuit_sampler.py", line 199, in convert
    param_bindings=p_b)
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\aqua\operators\converters\circuit_sampler.py", line 281, in sample_circuits
    had_transpiled=self._transpile_before_bind)
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\aqua\quantum_instance.py", line 273, in execute
    qobj = self.assemble(circuits)
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\aqua\quantum_instance.py", line 247, in assemble
    return compiler.assemble(circuits, **self._run_config.to_dict())
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\compiler\assemble.py", line 154, in assemble
    qobj_header=qobj_header, run_config=run_config)
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\assembler\assemble_circuits.py", line 266, in assemble_circuits
    qobj_config.memory_slots = max(memory_slot_sizes)
ValueError: max() arg is an empty sequence

Steps to reproduce the problem

Currently, the only thing which has a non-implemented to_circuit_op() method is OperatorStateFn, so constructing a circuit entirely comprised of OperatorStateFn raises the exception:

from qiskit.aqua.operators import X, Z, OperatorStateFn, PauliExpectation, CircuitSampler
from qiskit import Aer

circuit_expectation = PauliExpectation().convert(OperatorStateFn(Z).adjoint() @ OperatorStateFn(X))
sampler = CircuitSampler(backend=Aer.get_backend('qasm_simulator'))
sampled_op = sampler.convert(circuit_expectation)

Incidentally, you necessarily need to have some composition of OperatorStateFns to get the above error. If the operator to be converted contains only a single OperatorStateFn, you will get a NotImplementedError

Traceback (most recent call last):
  File "D:/qiskit advocate/test_project/main.py", line 8, in <module>
    sampled_op = sampler.convert(circuit_expectation)
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\aqua\operators\converters\circuit_sampler.py", line 178, in convert
    operator_dicts_replaced = operator.to_circuit_op()
  File "D:\python_venvs\qiskit_venv\lib\site-packages\qiskit\aqua\operators\state_fns\operator_state_fn.py", line 176, in to_circuit_op
    raise NotImplementedError
NotImplementedError

The reason for this is that the to_circuit_op() function in ListOp explicitly checks for OperatorStateFn and does not call its not implemented to_circuit_op() function, while in the case of a single OperatorStateFn the function on it is called directly in CircuitSampler.convert().

What is the expected behavior?

Raise a more descriptive error, or just work siletly.

Suggested solutions

  • First of all, I would suggest updating the docstring for CircuitSampler to explicitly state that it not only approximates the CircuitStateFns found in the given operator, but also converts anything else to circuits and approximates them as well (except of course OperatorStateFn, which does not support the conversion).
  • Explicitly check if circs is an empty list and do not call the self.sample_circuits in CircuitSampler.convert(). Maybe priniting a warning message would be helpfull for users, so that they know why nothing was converted in their operator, but I am not sure.
  • Alternatively, terra may want to improve the function assemble_circuits() in assembler/assemble_circuits.py to explicitly check for empty circuits and raise a descriptive error, and aqua might try-catch this error, instead of explicitly checking for empty circs
@molar-volume
Copy link
Contributor

molar-volume commented Oct 16, 2020

to_circuit_op() is defined for PrimitiveOp and ListOp, so maybe it can be defined straightforwardly for OperatorStateFn as well, something like

def to_circuit_op(self):
   csfn =  CircuitStateFn(self.primitive.to_circuit()) * self.coeff
   return csfn.adjoint() if self.is_measurement else csfn

@ikkoham
Copy link
Contributor

ikkoham commented Oct 22, 2020

@molar-volume Thanks. I think using primitive.to_circuit() for to_circuit_op() is not a desired behavior for OperatorStateFn.
For this case, we should validate inPauliExpectation().convert(), whether the input operator can be converted into an operator with the Pauli measurements`.

Do you continue this issue?

@molar-volume
Copy link
Contributor

Hi @ikkoham, I have not work on it since the pull request, you can go ahead, if you want to continue :)

@ikkoham
Copy link
Contributor

ikkoham commented Oct 22, 2020

OK, thanks

@ikkoham ikkoham self-assigned this Oct 22, 2020
@Cryoris Cryoris changed the title CircuitSampler fails for specific operators CircuitSampler fails on expressions consisting only of OperatorStateFns Oct 22, 2020
ikkoham added a commit to ikkoham/qiskit-aqua that referenced this issue Oct 26, 2020
This was referenced Oct 26, 2020
manoelmarques pushed a commit to manoelmarques/qiskit-aqua that referenced this issue Nov 9, 2020
* fix qiskit-community#1311

* fix qiskit-community#1317

* add a test of reps for trotterization

* fix tests for qiskit-community#1311

* fix typing

* fix qiskit-community#1321

* add release note

* fix qiskit-community#1381

* add a test for to_opflow

* Update qiskit/aqua/operators/converters/circuit_sampler.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* remove None from constructor

* simplify logic

* Revert "remove None from constructor"

This reverts commit c884ebb.

* add releasenote

* simplify logic (not iscomplex to isreal)

* remove Optional

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
mtreinish pushed a commit to mtreinish/qiskit-core that referenced this issue Nov 20, 2020
* fix qiskit-community/qiskit-aqua#1311

* fix qiskit-community/qiskit-aqua#1317

* add a test of reps for trotterization

* fix tests for qiskit-community/qiskit-aqua#1311

* fix typing

* fix qiskit-community/qiskit-aqua#1321

* add release note

* fix qiskit-community/qiskit-aqua#1381

* add a test for to_opflow

* Update qiskit/aqua/operators/converters/circuit_sampler.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* remove None from constructor

* simplify logic

* Revert "remove None from constructor"

This reverts commit c884ebba123adee22f252e11f90964a6defaec38.

* add releasenote

* simplify logic (not iscomplex to isreal)

* remove Optional

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
manoelmarques pushed a commit to manoelmarques/qiskit-terra that referenced this issue Dec 2, 2020
* fix qiskit-community/qiskit-aqua#1311

* fix qiskit-community/qiskit-aqua#1317

* add a test of reps for trotterization

* fix tests for qiskit-community/qiskit-aqua#1311

* fix typing

* fix qiskit-community/qiskit-aqua#1321

* add release note

* fix qiskit-community/qiskit-aqua#1381

* add a test for to_opflow

* Update qiskit/aqua/operators/converters/circuit_sampler.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* remove None from constructor

* simplify logic

* Revert "remove None from constructor"

This reverts commit c884ebba123adee22f252e11f90964a6defaec38.

* add releasenote

* simplify logic (not iscomplex to isreal)

* remove Optional

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
manoelmarques pushed a commit to manoelmarques/qiskit-terra that referenced this issue Dec 7, 2020
* fix qiskit-community/qiskit-aqua#1311

* fix qiskit-community/qiskit-aqua#1317

* add a test of reps for trotterization

* fix tests for qiskit-community/qiskit-aqua#1311

* fix typing

* fix qiskit-community/qiskit-aqua#1321

* add release note

* fix qiskit-community/qiskit-aqua#1381

* add a test for to_opflow

* Update qiskit/aqua/operators/converters/circuit_sampler.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>

* remove None from constructor

* simplify logic

* Revert "remove None from constructor"

This reverts commit c884ebba123adee22f252e11f90964a6defaec38.

* add releasenote

* simplify logic (not iscomplex to isreal)

* remove Optional

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

* Update qiskit/aqua/operators/legacy/weighted_pauli_operator.py

Co-authored-by: John Lapeyre <jlapeyre@users.noreply.github.com>
Co-authored-by: Panagiotis Barkoutsos <bpa@zurich.ibm.com>
Co-authored-by: Steve Wood <40241007+woodsp-ibm@users.noreply.github.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants