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

Transpilation fails with snapshot instruction #5556

Closed
yaelbh opened this issue Dec 23, 2020 · 2 comments · Fixed by #5554 or #5570
Closed

Transpilation fails with snapshot instruction #5556

yaelbh opened this issue Dec 23, 2020 · 2 comments · Fixed by #5554 or #5570
Labels
bug Something isn't working

Comments

@yaelbh
Copy link
Contributor

yaelbh commented Dec 23, 2020

Information

  • Qiskit Terra version: master
  • Python version: 3.8.5
  • Operating system: Linux

What is the current behavior?

Transpiling a simple circuit, which contains a snapshot, with basis gates ['u', 'cx'], raises an error. The same happens with more sets of basis gates: ['r', 'cz'], ['rz', 'rx', 'cz'], ['p', 'sx', 'cx']. However transpiling the same circuit with basis gates ['u3', 'cx'] is OK.

Note: this is working well with the stable version, the bug appears only in the master version.

Steps to reproduce the problem

from qiskit import QuantumCircuit, transpile
from qiskit.providers.aer import QasmSimulator
from qiskit.providers.aer.extensions import snapshot_statevector

backend = QasmSimulator()

circ = QuantumCircuit(1)
circ.z(0)
circ.snapshot_statevector('final')

transpile(circ, backend, basis_gates=['u3', 'cx'])
print("Transpilation with ['u3', 'cx'] is fine")

transpile(circ, backend, basis_gates=['u', 'cx'])
print("Transpilation with ['u', 'cx'] is fine")

results with

(YaelEnv) yaelbh@iris-quantum2:~/work/not_qiskit$ python snapshot_invalid.py 
/home/yaelbh/work/terra/System/qiskit/__init__.py:69: RuntimeWarning: Could not import the IBMQ provider from the qiskit-ibmq-provider package. Install qiskit-ibmq-provider or check your installation.
  warnings.warn('Could not import the IBMQ provider from the '
/opt/anaconda3/envs/YaelEnv/lib/python3.8/site-packages/qiskit/aqua/operators/operator_globals.py:48: DeprecationWarning: `from_label` is deprecated and will be removed no earlier than 3 months after the release date. Use Pauli(label) instead.
  X = make_immutable(PrimitiveOp(Pauli.from_label('X')))
Transpilation with ['u3', 'cx'] is fine
Traceback (most recent call last):
  File "snapshot_invalid.py", line 14, in <module>
    transpile(circ, backend, basis_gates=['u', 'cx'])
  File "/home/yaelbh/work/terra/System/qiskit/compiler/transpile.py", line 241, in transpile
    circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_args)))
  File "/home/yaelbh/work/terra/System/qiskit/tools/parallel.py", line 112, in parallel_map
    return [task(values[0], *task_args, **task_kwargs)]
  File "/home/yaelbh/work/terra/System/qiskit/compiler/transpile.py", line 324, in _transpile_circuit
    result = pass_manager.run(circuit, callback=transpile_config['callback'],
  File "/home/yaelbh/work/terra/System/qiskit/transpiler/passmanager.py", line 225, in run
    return self._run_single_circuit(circuits, output_name, callback)
  File "/home/yaelbh/work/terra/System/qiskit/transpiler/passmanager.py", line 288, in _run_single_circuit
    result = running_passmanager.run(circuit, output_name=output_name, callback=callback)
  File "/home/yaelbh/work/terra/System/qiskit/transpiler/runningpassmanager.py", line 113, in run
    dag = self._do_pass(pass_, dag, passset.options)
  File "/home/yaelbh/work/terra/System/qiskit/transpiler/runningpassmanager.py", line 144, in _do_pass
    dag = self._run_this_pass(pass_, dag)
  File "/home/yaelbh/work/terra/System/qiskit/transpiler/runningpassmanager.py", line 156, in _run_this_pass
    new_dag = pass_.run(dag)
  File "/home/yaelbh/work/terra/System/qiskit/transpiler/passes/optimization/optimize_1q_decomposition.py", line 87, in run
    operator = Operator(qc)
  File "/home/yaelbh/work/terra/System/qiskit/quantum_info/operators/operator.py", line 85, in __init__
    self._data = self._init_instruction(data).data
  File "/home/yaelbh/work/terra/System/qiskit/quantum_info/operators/operator.py", line 496, in _init_instruction
    op._append_instruction(instruction)
  File "/home/yaelbh/work/terra/System/qiskit/quantum_info/operators/operator.py", line 554, in _append_instruction
    self._append_instruction(instr, qargs=new_qargs)
  File "/home/yaelbh/work/terra/System/qiskit/quantum_info/operators/operator.py", line 532, in _append_instruction
    raise QiskitError('Cannot apply Instruction: {}'.format(obj.name))
qiskit.exceptions.QiskitError: 'Cannot apply Instruction: snapshot'
@yaelbh yaelbh added the bug Something isn't working label Dec 23, 2020
@enavarro51
Copy link
Contributor

enavarro51 commented Dec 23, 2020

Oddly enough, I was just working on this as part of #5554. This is caused by Optimize1qGatesDecomposition.run() calling Operator(qc). Operator will fail for 'snapshot', 'delay', and 'reset'. The reason it works with 'u3' in the basis is that if the basis has 'u1', 'u2', or 'u3', the optimizer calls Optimize1qGates.run() instead, which does not call Operator().

It should run ok with optimization_level=0 for transpile.

@mtreinish
Copy link
Member

This is actually a duplicate of #5543 the issue is that the run collection for 1q gates is treating some non-gate 1q instructions as gates and trying to make an operator from that which obviously doesn't work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants