-
-
Notifications
You must be signed in to change notification settings - Fork 166
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
Error for when passing folded circuit to calculate expectation value in qiskit #1150
Comments
Hello @anhdpham, thank you for your interest in Mitiq! |
Thanks @anhdpham for reporting this! |
@anhdpham thanks for opening this and the other issue. If it could help clarify them, you're welcome to join the Mitiq community call on Unitary Fund's Discord, which is on Fridays at 18:00 CET (https://discord.com/invite/JqVGmpkP96). |
Hi @anhdpham, could you provide more details on your I attempted to reproduce this error but was unable to with my interpretation of the provided code (plus a few fast/unchecked assumptions from tutorials), to yield ~
|
Hi @Aaron-Robertson. My dependancies from !pip list: Package Version anyio 2.2.0 Here is my code that created the error in the last line when trying to evaluate the expectation value of the folded circuit using qiskit. import pylab H2_op = (-1.052373245772859 * I ^ I) + (0.39793742484318045 * I ^ Z) + (-0.39793742484318045 * Z ^ I) + def trial_circuit(parameter): scale_factors = [1., 1.5, 2., 2.5, 3.] def qiskit_executor_no_noise(circuit: qiskit.QuantumCircuit, shots: int = 8192) -> float: qiskit_executor_no_noise(trial_circuit([0,0,0,0])) qiskit_executor_no_noise(folded_circuits[0]) |
@anhdpham I see! I reproduced the error with |
@Aaron-Robertson Thanks. Will retest the program using a qiskit=0.32.1 |
Closing with documentation issue noted in #1201 |
I think there is a problem with passing a folded circuit generated by mitiq to the expectation value routine used to calculate in mitiq
def ansatz(parameter):
circuit = QuantumCircuit(2)
circuit.rx(parameter[0],0)
circuit.cnot(0,1)
circuit.rx(parameter[0],1)
circuit.cnot(0,1)
circuit.rx(parameter[0],0)
return circuit
angles = []
for angle in np.linspace(0, 2 * np.pi, 50):
angles.append(angle)
circuits = []
for angle in angles:
circuits.append(ansatz([angle]))
def qiskit_executor(circuit: qiskit.QuantumCircuit, shots: int = 8192) -> float:
quantum_instance = QuantumInstance(backend=Aer.get_backend('qasm_simulator'),noise_model = noise_model,
basis_gates = noise_model.basis_gates, shots=10000)
psi = CircuitStateFn(circuit)
measurable_expression = StateFn(Hamiltonian, is_measurement=True).compose(psi)
expectation = PauliExpectation().convert(measurable_expression)
sampler = CircuitSampler(quantum_instance).convert(expectation)
value = sampler.eval().real
return value
scale_factors = [1., 1.5, 2., 2.5, 3.]
folded_circuits = [
zne.scaling.fold_gates_at_random(circuits[0], scale)
for scale in scale_factors
]
qiskit_executor(folded_circuits[0])
Error:
ValueError Traceback (most recent call last)
/tmp/ipykernel_300/1422248828.py in
----> 1 qiskit_executor(folded_circuits[0])
/tmp/ipykernel_300/1414635650.py in qiskit_executor(circuit, shots)
4 # ansatz_value = ansatz.bind_parameters({theta: val for val in theta_val})
5 psi = CircuitStateFn(circuit)
----> 6 measurable_expression = StateFn(Hamiltonian, is_measurement=True).compose(psi)
7 expectation = PauliExpectation().convert(measurable_expression)
8 sampler = CircuitSampler(quantum_instance).convert(expectation)
~/anaconda3/envs/IBM_qiskit/lib/python3.9/site-packages/qiskit/opflow/state_fns/state_fn.py in compose(self, other, permutation, front)
308 )
309
--> 310 new_self, other = self._expand_shorter_operator_and_permute(other, permutation)
311
312 if front:
~/anaconda3/envs/IBM_qiskit/lib/python3.9/site-packages/qiskit/opflow/state_fns/state_fn.py in _expand_shorter_operator_and_permute(self, other, permutation)
263 return self, StateFn("0" * self.num_qubits)
264
--> 265 return super()._expand_shorter_operator_and_permute(other, permutation)
266
267 def to_matrix(self, massive: bool = False) -> np.ndarray:
~/anaconda3/envs/IBM_qiskit/lib/python3.9/site-packages/qiskit/opflow/operator_base.py in _expand_shorter_operator_and_permute(self, other, permutation)
436 other = Zero.class("0" * self.num_qubits)
437 elif other.num_qubits < self.num_qubits:
--> 438 other = other._expand_dim(self.num_qubits - other.num_qubits)
439 elif other.num_qubits > self.num_qubits:
440 new_self = self._expand_dim(other.num_qubits - self.num_qubits)
~/anaconda3/envs/IBM_qiskit/lib/python3.9/site-packages/qiskit/opflow/state_fns/circuit_state_fn.py in _expand_dim(self, num_qubits)
383 # this is equivalent to self.tensor(identity_operator), but optimized for better performance
384 # just like in tensor method, qiskit endianness is reversed here
--> 385 return self.permute(list(range(num_qubits, num_qubits + self.num_qubits)))
386
387 def permute(self, permutation: List[int]) -> "CircuitStateFn":
~/anaconda3/envs/IBM_qiskit/lib/python3.9/site-packages/qiskit/opflow/state_fns/circuit_state_fn.py in permute(self, permutation)
396 A new CircuitStateFn containing the permuted circuit.
397 """
--> 398 new_qc = QuantumCircuit(max(permutation) + 1).compose(self.primitive, qubits=permutation)
399 return CircuitStateFn(new_qc, coeff=self.coeff, is_measurement=self.is_measurement)
ValueError: max() arg is an empty sequence
It works fine if i pass a circuit created by QuantumCircuit in qiskit.
The text was updated successfully, but these errors were encountered: