Main Content

unpack

Unpack composite gates in quantum circuit

Since R2023a

Installation Required: This functionality requires MATLAB Support Package for Quantum Computing.

Description

uc = unpack(c) unpacks each composite gate in the input circuit into its containing gates and returns a new quantum circuit. The returned circuit can still contain composite gates if the input circuit contains composite gates that are defined recursively.

example

uc = unpack(c,id) unpacks the composite gates with indices id in c.Gates.

example

uc = unpack(c,"recursive") unpacks every composite gate into its containing gates recursively. The returned uc.Gates contains no CompositeGate objects.

example

Examples

collapse all

Unpack composite gates into their internal gates in a quantum circuit.

First, create an inner circuit that consists of a controlled X gate and an x-axis rotation gate. This circuit will be reused as a composite gate.

cInner = quantumCircuit([cxGate(1,2), rxGate(2,pi/3)]);

Create a circuit that consists of a Hadamard gate and two composite gates that are constructed from the previous inner circuit.

gates = [hGate(1)
         compositeGate(cInner,[1 2])
         compositeGate(cInner,[3 1])];
c = quantumCircuit(gates);
c.Gates
ans = 

  3×1 QuantumGate array with gates:

       Id   Gate        Control   Target   NumGates
       1    h                     1                
       2    composite             [1,2]    2       
       3    composite             [3,1]    2       

Create a circuit where all composite gates are unpacked. Show the gates of this new circuit.

uc = unpack(c);
uc.Gates
ans = 

  5×1 SimpleGate array with gates:

    Id   Gate   Control   Target   Angle
     1   h                1             
     2   cx     1         2             
     3   rx               2        pi/3 
     4   cx     3         1             
     5   rx               1        pi/3 

Create another circuit where only the composite gate with index 3 from the original circuit c is unpacked. Show the gates of this circuit.

uc3 = unpack(c,3);
uc3.Gates
ans = 

  4×1 QuantumGate array with gates:

       Id   Gate        Control   Target   Angle   NumGates
       1    h                     1                        
       2    composite             [1,2]            2       
       3    cx          3         1                        
       4    rx                    1        pi/3            

Unpack composite gates in a quantum circuit into their internal gates recursively and return an equivalent circuit without the composite gates.

First, create a quantum circuit that contains nested composite gates.

cInner = quantumCircuit([cxGate(1,2),rxGate(2,pi/3)],Name="inner");
gates = [hGate(1); compositeGate(cInner,[1 2])];
cMiddle = quantumCircuit(gates,3,Name="middle");
gatesOuter = [compositeGate(cInner,[3 5])
              hGate(3)
              compositeGate(cMiddle,[4 5 1])];
cOuter = quantumCircuit(gatesOuter,Name="outer");

Show the gates of this circuit. The outer circuit contains two composite gates named inner and middle that were constructed from the inner and middle circuits.

cOuter.Gates
ans = 

  3×1 QuantumGate array with gates:

       Id   Gate     Control   Target    NumGates
       1    inner              [3,5]     2       
       2    h                  3                 
       3    middle             [4,5,1]   2       

Create a circuit where all composite gates are unpacked once. The new circuit still contains a composite gate named inner from the middle circuit that was unpacked once.

uc = unpack(cOuter);
uc.Gates
ans = 

  5×1 QuantumGate array with gates:

       Id   Gate    Control   Target   Angle   NumGates
       1    cx      3         5                        
       2    rx                5        pi/3            
       3    h                 3                        
       4    h                 4                        
       5    inner             [4,5]            2       

Create another circuit where all composite gates are unpacked recursively. The circuit does not contain composite gates because all composite gates are replaced by their internal gates.

ucRecursive = unpack(cOuter,"recursive");
ucRecursive.Gates
ans = 

  6×1 SimpleGate array with gates:

    Id   Gate   Control   Target   Angle
     1   cx     3         5             
     2   rx               5        pi/3 
     3   h                3             
     4   h                4             
     5   cx     4         5             
     6   rx               5        pi/3 

Input Arguments

collapse all

Quantum circuit, specified as a quantumCircuit object.

Indices of composite gates to unpack, specified as a scalar or a vector of positive integers. Specify id as an index or indices into c.Gates of composite gates to unpack.

Version History

Introduced in R2023a