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

Allow combining circuits with different unique registers #389

Merged
merged 2 commits into from
Apr 10, 2018

Conversation

chriseclectic
Copy link
Member

@chriseclectic chriseclectic commented Apr 6, 2018

Description

This patch modifies the QuantumCircuit.combine and QuantumCircuit.extend functions (and hence +, and += operators) to allow combing circuits with common and/or distinct quantum and classical registers.

Example 1 (distinct registers)

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

qr1 = QuantumRegister('q1', 2)
cr1 = ClassicalRegister('c1', 2)
qr2 = QuantumRegister('q2', 2)
cr2 = ClassicalRegister('c2', 2)

# Circ 1
qc1 = QuantumCircuit(qr1, cr1)
qc1.h(qr1)

# Circ 2
qc2 = QuantumCircuit(qr2, cr2)
qc2.x(qr2)

# Combine and add additional gates
circ = qc1 + qc2
circ.cx(qr1, qr2)
circ.measure(qr1, cr1)
circ.measure(qr2, cr2)

In this case the resulting qasm equivalents will be

# qc1
OPENQASM 2.0;
include "qelib1.inc";
qreg q1[2];
creg c1[2];
h q1[0];
h q1[1];

# qc2
OPENQASM 2.0;
include "qelib1.inc";
qreg q2[2];
creg c2[2];
x q2[0];
x q2[1];

# circ
OPENQASM 2.0;
include "qelib1.inc";
qreg q1[2];
creg c1[2];
creg c2[2];
qreg q2[2];
h q1[0];
h q1[1];
x q2[0];
x q2[1];
cx q1[0],q2[0];
cx q1[1],q2[1];
measure q1[0] -> c1[0];
measure q1[1] -> c1[1];
measure q2[0] -> c2[0];
measure q2[1] -> c2[1];

Example 2 (common register)

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

qr1 = QuantumRegister('q1', 1)
qr2 = QuantumRegister('q2', 1)
cr = ClassicalRegister('c', 2)

# Circ 1
qc1 = QuantumCircuit(qr1, cr)
qc1.h(qr1)

# Circ 2
qc2 = QuantumCircuit(qr2, cr)
qc2.x(qr2)

# Combine and add additional gates
circ = qc1 + qc2
circ.cx(qr1, qr2)
circ.measure(qr1[0], cr[0])
circ.measure(qr2[0], cr[1])

In this case the resulting qasm equivalents will be

# qc1
OPENQASM 2.0;
include "qelib1.inc";
qreg q1[1];
creg c[2];
h q1[0];

# qc2
OPENQASM 2.0;
include "qelib1.inc";
qreg q2[1];
creg c[2];
x q2[0];

# circ
OPENQASM 2.0;
include "qelib1.inc";
qreg q1[1];
creg c[2];
qreg q2[1];
h q1[0];
x q2[0];
cx q1[0],q2[0];
measure q1[0] -> c[0];
measure q2[0] -> c[1];

Example 3 (raise an error)

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

qr1 = QuantumRegister('reg', 1)
qr2 = QuantumRegister('reg', 2)
cr1 = ClassicalRegister('reg', 2)

qc1 = QuantumCircuit(qr1)
qc2 = QuantumCircuit(qr2)
qc3 = QuantumCircuit(cr1)

qc1 + qc2 # raises QISKitError due to registers having same name but different size
qc2 + qc3 # raises QISKitError due to registers having same name but different type

Motivation and Context

When working with QuantumCircuit objects directly you should be able to combine circuits which are defined on different quantum and/or classical registers. In the case where two circuits have registers with common names these should be the same register or an error is raised.

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@chriseclectic chriseclectic force-pushed the combine-circuits-patch branch from c43824b to ec057fa Compare April 6, 2018 22:17
Copy link
Member

@ajavadia ajavadia left a comment

Choose a reason for hiding this comment

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

Thanks for the useful patch and the thorough explanation. It looks good to merge to me.
We can now combine circuits across both width and depth.

Copy link
Member

@awcross1 awcross1 left a comment

Choose a reason for hiding this comment

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

Looks good!

@ajavadia ajavadia merged commit ffc46b3 into Qiskit:master Apr 10, 2018
@chriseclectic chriseclectic deleted the combine-circuits-patch branch September 14, 2018 21:07
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
* modify circuit addition rules to allow combining circuits with different but unique registers

* fixed bug in extend circuit, added unit tests for extend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants